还是判断两线段相交,水题。
郁闷的是,我加了精度判断过不去,没加反而A掉了。。。好纠结。。
明白了,我的精度加的不是位置T T 。。。if( d1 * d2 < -eps && d3 * d4 < -eps )这个地方,我开始加的是if( d1 * d2 < eps && d3 * d4 < eps ) 如果其中d1 d3为0,这个就成立啦,但是其实它有可能不成立T T 。。。笨死啦小媛。。。不过自己想清楚的~嘻嘻
#include <queue> #include <stack> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <limits.h> #include <string.h> #include <algorithm> using namespace std; const double eps = 0.00; typedef struct Point{ double x,y; }Point; typedef struct Line{ Point a,b; }Line; double Direction(Point a, Point b, Point c ) { return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y); } bool onSegment(Point a, Point b, Point c) { double maxx = max(a.x,b.x); double maxy = max(a.y,b.y); double minx = min(a.x,b.x); double miny = min(a.y,b.y); if( c.x >= minx && c.x <= maxx && c.y >= miny && c.y <= maxy ) return true; return false; } bool segIntersect(Point p1,Point p2, Point p3, Point p4) { double d1 = Direction(p3,p4,p1); double d2 = Direction(p3,p4,p2); double d3 = Direction(p1,p2,p3); double d4 = Direction(p1,p2,p4); if( d1 * d2 < eps && d3 * d4 < eps ) return true; if( d1 == 0.0 && onSegment(p3,p4,p1) ) return true; if( d2 == 0.0 && onSegment(p3,p4,p2) ) return true; if( d3 == 0.0 && onSegment(p1,p2,p3) ) return true; if( d4 == 0.0 && onSegment(p1,p2,p4) ) return true; return false; } Line l[2010]; int main() { int n; while( ~scanf("%d",&n) ) { for(int i=0; i<n; i++) scanf("%lf %lf %lf %lf",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y); for(int i=0; i<n; i++) for(int k=i+1; k<n; k++) if( segIntersect(l[i].a,l[i].b,l[k].a,l[k].b) ) { printf("burned!/n"); goto end; } printf("ok!/n"); end:; } return 0; }