Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为何C过了,而GCC过不了WA,求解释#include <math.h> #define eps 1e-8 #define iszero(x) (fabs(x) < eps) #define opsign(x, y) ((x) > 0 && (y) < 0 || (x) < 0 && (y) > 0) typedef struct s_point { double x, y; } Point; typedef struct s_point Vector; Point point(double x, double y) { Point p; p.x = x, p.y = y; return p; } double cross_product(Vector* pv1, Vector* pv2) { return pv1->x * pv2->y - pv1->y * pv2->x; } double direction(Point* p1, Point* p2, Point* p) { Vector v1 = point(p2->x - p1->x, p2->y - p1->y); Vector v2 = point(p->x - p1->x, p->y - p1->y); return cross_product(&v1, &v2); } int segment_intersect(Point* s1p1, Point* s1p2, Point* s2p1, Point* s2p2) { double d1 = direction(s1p1, s1p2, s2p1); double d2 = direction(s1p1, s1p2, s2p2); double d3 = direction(s2p1, s2p2, s1p1); double d4 = direction(s2p1, s2p2, s1p2); if (opsign(d1, d2) && opsign(d3, d4)) return 1; /*if ((iszero(d1) || iszero(d2)) && opsign(d3, d4)) return 1; if ((iszero(d3) || iszero(d4)) && opsign(d1, d2)) return 1; if ((iszero(d1) || iszero(d2)) && (iszero(d3) || iszero(d4))) return 1;*/ return 0; } double distance(Point* p1, Point* p2) { double dx = p2->x - p1->x; double dy = p2->y - p1->y; return sqrt(dx*dx+dy*dy); } #include <stdio.h> #include <string.h> #define INF 200.0 #define N 20 #define M 80 double x[N], y[N][6]; double cost[M][M]; double dist[M]; char found[M]; int n, m; Point ps = {0.0, 5.0}, pt = {10.0, 5.0}; void calc(Point* p1, Point* p2, int u, int v, int i, int j) { // [i, j) int k, flag = 0; for (k = i; k < j; k++) { Point p3 = point(x[k], y[k][0]); Point p4 = point(x[k], y[k][1]); Point p5 = point(x[k], y[k][2]); Point p6 = point(x[k], y[k][3]); Point p7 = point(x[k], y[k][4]); Point p8 = point(x[k], y[k][5]); if (segment_intersect(p1, p2, &p3, &p4) || segment_intersect(p1, p2, &p5, &p6) || segment_intersect(p1, p2, &p7, &p8)) { flag = 1; break; } } if (flag) cost[u][v] = cost[v][u] = INF; else { cost[u][v] = cost[v][u] = distance(p1, p2); } } int main() { while (scanf("%d", &n) != EOF && n != -1) { int i, j, s, t; for (i = 0; i < n; i++) { scanf("%lf%lf%lf%lf%lf", &x[i], &y[i][1], &y[i][2], &y[i][3], &y[i][4]); y[i][0] = 0.0, y[i][5] = 10.0; } m = 2 + 4 * n; for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { cost[i][j] = INF; } cost[i][i] = 0.0; } for (i = 0; i < n; i++) { for (j = i+1; j < n; j++) { for (s = 1; s <= 4; s++) { for (t = 1; t <= 4; t++) { Point p1 = point(x[i], y[i][s]); Point p2 = point(x[j], y[j][t]); int u = i * 4 + s; int v = j * 4 + t; calc(&p1, &p2, u, v, i+1, j); } } } } for (i = 0; i < n; i++) { for (s = 1; s <= 4; s++) { Point p = point(x[i], y[i][s]); int u = 0, v = i*4 + s; calc(&ps, &p, u, v, 0, i); } } for (i = 0; i < n; i++) { for (s = 1; s <= 4; s++) { Point p = point(x[i], y[i][s]); int u = m-1, v = i*4 + s; calc(&p, &pt, u, v, i+1, n); } } calc(&ps, &pt, 0, m-1, 0, n); for (i = 0; i < m; i++) { dist[i] = INF; } dist[0] = 0.0; memset(found, 0, m); for (i = 0; i < m; i++) { double min = INF; int mini; for (j = 0; j < m; j++) { if (!found[j] && (dist[j] < min || iszero(dist[j]-min))) { min = dist[j], mini = j; } } found[mini] = 1; for (j = 0; j < m; j++) { double tmp = dist[mini] + cost[mini][j]; if (tmp < dist[j]) dist[j] = tmp; } } printf("%.2lf\n", dist[m-1]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator