user2590945
user2590945

Reputation: 1

Strange behaviour of struct in C++

I was solving problem 10263 (Railway) of uva online judge (http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1204) and getting Wrong Answer for my code. After discussing it with a friend (who also solved the problem and got Accepted) he took a look at my code and it was extremely similar to his (all the geometry functions were the same, for example, since we are training as a team for ICPC and using the same code libraries).

So, after I started trying one change at a time to see what the problem was, I found this strange behaviour.

My initial code (which got Wrong Answer) is this one:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <limits>
#define EPS 1e-9
using namespace std;

struct point {
    double x, y;
    point(double _x, double _y) { x = _x, y = _y; } //EDITED
    point() { x = 0.0, y = 0.0; }
};

double dist(point p1, point p2) {
    return hypot(p1.x - p2.x, p1.y - p2.y); 
}

double distToLine(point p, point A, point B, point *c) {
    double scale = (double) ((p.x - A.x) * (B.x - A.x) + (p.y - A.y) * (B.y - A.y)) / ((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y));
    c->x = A.x + scale * (B.x - A.x);
    c->y = A.y + scale * (B.y - A.y);
    return dist(p, *c); 
}

double distToLineSegment(point p, point A, point B, point* c) {
    if ((B.x-A.x) * (p.x-A.x) + (B.y-A.y) * (p.y-A.y) < EPS) {
        c->x = A.x; c->y = A.y;
        return dist(p, A); 
    }
    if ((A.x-B.x) * (p.x-B.x) + (A.y-B.y) * (p.y-B.y) < EPS) {
        c->x = B.x; c->y = B.y; 
        return dist(p, B); 
    } 
    return distToLine(p, A, B, c); 
}

int main() {
    int Mx, My;
    while (cin >> Mx) {
        int N, x, y;
        cin >> My >> N;

        point A, B, aux, res, M(Mx, My);
        double dres = numeric_limits<double>::infinity(), d;

        cin >> x >> y;
        A = point(x,y);

        for (int i=0; i<N; i++) {
            cin >> x >> y;
            B = point(x,y);

            d = distToLineSegment(M, A, B, &aux);
            if (d < dres) {
                dres = d;
                res = aux;
            }
            A = B;
        }


        printf("%.4f\n%.4f\n",res.x,res.y);
    }
}

Now, after changing this lines:

int Mx, My;
    while (cin >> Mx) {
        int N, x, y;
        cin >> My >> N;

        point A, B, aux, res, M(Mx, My);

To

point M;
    while (cin >> M.x) {
        int N, x, y;
        cin >> M.y >> N;

        point A, B, aux, res;

My solution got accepted. Could anyone please help me understand what was the issue with reading the int values and then creating the point? In what possible causes could this code "misbehave" and generate different answers than the other one? Also, any ideas on why "M" was the only problem? (I kept reading "A" and "B" the same way as before and it didn't affect the answer, only the way I was reading "M" had to be changed).

Any hints to help me understand what is going on would be much appreciated!


Edit: Wrote the constructor wrong when "changing it back to the wrong version", sorry.

Upvotes: 0

Views: 206

Answers (1)

Manu343726
Manu343726

Reputation: 14174

The difference is wich @jhonchen902 suggest in the comment: The problem specifies output must be floating-point numbers with four digits after the decimal point.

In your first version, you where using integers.

Upvotes: 1

Related Questions