zoj 1648 判断线段是否相交
2014-07-23 09:02
381 查看
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=648
Circuit BoardTime Limit: 2 Seconds Memory Limit: 65536 KB
On the circuit board, there are lots of circuit paths. We know the basic constrain is that no two path cross each other, for otherwise the board will be burned.
Now given a circuit diagram, your task is to lookup if there are some crossed paths. If not find, print "ok!", otherwise "burned!" in one line.
A circuit path is defined as a line segment on a plane with two endpoints p1(x1,y1) and p2(x2,y2).
You may assume that no two paths will cross each other at any of their endpoints.
Input
The input consists of several test cases. For each case, the first line contains an integer n(<=2000), the number of paths, then followed by n lines each with four float numbers x1, y1, x2, y2.
Output
If there are two paths crossing each other, output "burned!" in one line; otherwise output "ok!" in one line.
Sample Input
1
0 0 1 1
2
0 0 1 1
0 1 1 0
Sample Output
ok!
burned!
。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/
模板题~~~
Circuit BoardTime Limit: 2 Seconds Memory Limit: 65536 KB
On the circuit board, there are lots of circuit paths. We know the basic constrain is that no two path cross each other, for otherwise the board will be burned.
Now given a circuit diagram, your task is to lookup if there are some crossed paths. If not find, print "ok!", otherwise "burned!" in one line.
A circuit path is defined as a line segment on a plane with two endpoints p1(x1,y1) and p2(x2,y2).
You may assume that no two paths will cross each other at any of their endpoints.
Input
The input consists of several test cases. For each case, the first line contains an integer n(<=2000), the number of paths, then followed by n lines each with four float numbers x1, y1, x2, y2.
Output
If there are two paths crossing each other, output "burned!" in one line; otherwise output "ok!" in one line.
Sample Input
1
0 0 1 1
2
0 0 1 1
0 1 1 0
Sample Output
ok!
burned!
。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/
模板题~~~
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <iostream> #include <algorithm> #define eps 1e-6 struct point { double x,y; }; struct beline { point a,b; }; using namespace std; point p[5000]; bool dy(double x,double y) { return x > y+eps; } bool xy(double x,double y) { return x < y-eps; } bool xyd(double x,double y) { return x < y+eps; } bool dyd(double x,double y) { return x > y-eps; } double dd(double x,double y) { return fabs(x-y) < eps; } double crossProduct(point a,point b,point c) { return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x); } 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(dd(crossProduct(a,b,c),0.0)&&dyd(c.x,minx)&&xyd(c.x,maxx) &&dyd(c.y,miny)&&xyd(c.y,maxy)) return true; return false; } bool segIntersect(point p1,point p2,point p3,point p4) { double d1 = crossProduct(p3,p4,p1); double d2 = crossProduct(p3,p4,p2); double d3 = crossProduct(p1,p2,p3); double d4 = crossProduct(p1,p2,p4); if(xy(d1*d2,0.0)&&xy(d3*d4,0.0)) return true; if(dd(d1,0.0)&&onSegment(p3,p4,p1)) return true; if(dd(d2,0.0)&&onSegment(p3,p4,p2)) return true; if(dd(d3,0.0)&&onSegment(p1,p2,p3)) return true; if(dd(d4,0.0)&&onSegment(p1,p2,p4)) return true; return false; } int main() { beline p[5000]; int n,i,j; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&p[i].a.x,&p[i].a.y,&p[i].b.x,&p[i].b.y); } if(n<=1) { printf("ok!\n"); continue; } bool flag=false; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(segIntersect(p[i].a,p[i].b,p[j].a,p[j].b)) { flag=true; break; } } } if(flag) { printf("burned!\n"); } else { printf("ok!\n"); } } return 0; }
相关文章推荐
- zoj 1648 Circuit Board (判断线段是否相交)
- zju1648 判断两条线段是否相交
- zoj 1010 Area 判断线段是否相交(把线段扩充一倍后 好处理) + 多边形求面积
- ZOJ 1648:Circuit Board __判断两线段相交
- ZOJ 1648 Circuit Board【跨立实验判断线段相交】
- ZOJ 1648 Circuit Board(判断线段相交)
- 判断两条线段是否相交(三种算法)
- 判断两个线段是否相交
- Circuit Board(zju1648,判断线段相交)
- 两条线段知道端点line1(x1,y1)(x2,y2)line2(x3,y3)(x4,y4),判断两条线段是否相交,交点坐标(x,y)
- hdu 1086(判断两线段是否相交)
- 已知两条线段端点,判断是否相交及交点
- 判断两个线段是否相交
- [Anonymous]判断线段是否相交
- 判断两个线段是否相交
- POJ 1410 Intersection(判断线段和矩形是否相交)
- 线段 ——2D(如何判断线段是否相交)
- hdu 1558 Segment set 并查集 叉积判断线段是否相交
- 判断点是否在直线上或线段上 判断线段相交 点与多边形的位置关系
- 德莱联盟(判断两个线段是否相交)