首页 > 代码库 > 【HDOJ】2150 Pipe

【HDOJ】2150 Pipe

计算几何的基础题目。是时候刷刷计算几何了。

  1 /* 2150 */  2 #include <cstdio>  3 #include <cstring>  4 #include <cstdlib>  5   6 typedef struct {  7     int x, y;  8 } Point_t;  9  10 typedef struct { 11     Point_t b, e; 12     int v; 13 } Pipe_t; 14  15 #define MAXN 105 16  17 Point_t points[MAXN]; 18 Pipe_t pipes[MAXN*30]; 19  20 int max(int a, int b) { 21     return a>b ? a:b; 22 } 23  24 int min(int a, int b) { 25     return a<b ? a:b; 26 } 27  28  29 int direction(Point_t p0, Point_t p1, Point_t p2) { 30     return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y); 31 } 32  33 bool onSegment(Point_t p0, Point_t p1, Point_t p2) { 34     if ( (min(p0.x,p1.x)<=p2.x && p2.x<=max(p0.x,p1.x)) && ((min(p0.y,p1.y)<=p2.y && p2.y<=max(p0.y,p1.y))) ) 35         return true; 36     return false; 37 } 38  39 bool intersect(Pipe_t x, Pipe_t y) { 40     if (x.v == y.v) 41         return false; 42     Point_t p1 = x.b, p2 = x.e, p3 = y.b, p4 = y.e; 43     int d1 = direction(p3, p4, p1); 44     int d2 = direction(p3, p4, p2); 45     int d3 = direction(p1, p2, p3); 46     int d4 = direction(p1, p2, p4); 47     if ( ((d1>0 && d2<0) || (d1<0 && d2>0)) && ((d3<0 && d4>0) || (d3>0 && d4<0)) ) 48         return true; 49     else if (d1==0 && onSegment(p3, p4, p1)) 50         return true; 51     else if (d2==0 && onSegment(p3, p4, p2)) 52         return true; 53     else if (d3==0 && onSegment(p1, p2, p3)) 54         return true; 55     else if (d4==0 && onSegment(p1, p2, p4)) 56         return true; 57     else 58         return false; 59 } 60  61 int main() { 62     int n, m; 63     int i, j, k; 64     bool flag; 65      66     #ifndef ONLINE_JUDGE 67         freopen("data.in", "r", stdin); 68     #endif 69      70     while (scanf("%d", &n) != EOF) { 71         m = 0; 72         flag = true; 73         for (i=0; i<n; ++i) { 74             scanf("%d", &k); 75             for (j=0; j<k; ++j) 76                 scanf("%d %d", &points[j].x, &points[j].y); 77             for (j=1; j<k; ++j)    { 78                 pipes[m].b = points[j-1]; 79                 pipes[m].e = points[j]; 80                 pipes[m].v = i; 81                 ++m; 82             } 83         } 84         for (i=0; i<m; ++i) { 85             for (j=i+1; j<m; ++j) { 86                 if (intersect(pipes[i], pipes[j])) { 87                     flag = false; 88                     goto _output; 89                 } 90             } 91         } 92         _output: 93         if (flag) 94             printf("No\n"); 95         else 96             printf("Yes\n"); 97     } 98  99     return 0;100 }

 

【HDOJ】2150 Pipe