您的位置:首页 > 产品设计 > UI/UE

ZOJ 1648:Circuit Board __判断两线段相交

2012-08-06 14:05 369 查看
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<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 2005
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)

struct point
{
double x,y;
};
struct line
{
point sp,ep;
};
line a[maxn];

double x_multi(point p1,point p2,point p3)  //叉积
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);
}

bool Onsegment(point p1,point p2,point p3)  //快速排斥实验
{
double min_x=min(p1.x,p2.x);
double min_y=min(p1.y,p2.y);
double max_x=max(p1.x,p2.x);
double max_y=max(p1.y,p2.y);
if(p3.x>=min_x&&p3.x<=max_x&&p3.y>=min_y&&p3.y<=max_y)
return true;
return false;
}

bool Is_intersected(point p1,point p2,point p3,point p4)
{
double d1=x_multi(p1,p2,p3);
double d2=x_multi(p1,p2,p4);
double d3=x_multi(p3,p4,p1);
double d4=x_multi(p3,p4,p2);
if(d1*d2<0.0&&d3*d4<0.0)
return true;
if(d1==0.0&&Onsegment(p1,p2,p3))  //跨立实验
return true;
if(d2==0.0&&Onsegment(p1,p2,p4))
return true;
if(d3==0.0&&Onsegment(p3,p4,p1))
return true;
if(d4==0.0&&Onsegment(p3,p4,p2))
return true;
return false;
}

int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&a[i].sp.x,&a[i].sp.y,&a[i].ep.x,&a[i].ep.y);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(Is_intersected(a[i].sp,a[i].ep,a[j].sp,a[j].ep))
{
puts("burned!");
goto end;
}
}
puts("ok!");
end:;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: