ZOJ 1648Circuit Board

来源:互联网 发布:淘宝物流显示香港 编辑:程序博客网 时间:2024/06/05 11:46

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<queue>#include<cmath>#include<map>#include<algorithm>using namespace std;typedef long long LL;#define rep(i,j,k) for (int i=j;i<=k;i++)#define per(i,j,k) for (int i=j;i>=k;i--)#define inone(x) scanf("%d",&x)#define intwo(x,y) scanf("%d%d",&x,&y)typedef pair<double,double> pii;const int N = 2005;const int INF=0x7FFFFFFF;int n;pii a[N],b[N];int check(int x,int y){    double l=max(a[x].first,a[y].first);    double r=min(b[x].first,b[y].first);    if (l > r) return 0;    double ll=max(min(a[x].second,b[x].second),min(a[y].second,b[y].second));    double rr=min(max(a[x].second,b[x].second),max(a[y].second,b[y].second));    if (ll > rr) return 0;    if (a[x].first==b[x].first) return 1;    if (a[y].first==b[y].first) return 1;    double k1=(a[x].second-b[x].second)/(a[x].first-b[x].first);    double k2=(a[y].second-b[y].second)/(a[y].first-b[y].first);    double d1=a[x].second+k1*(l-a[x].first)-a[y].second-k2*(l-a[y].first);    double d2=a[x].second+k1*(r-a[x].first)-a[y].second-k2*(r-a[y].first);    return d1*d2<=0;}int main(){    while (~inone(n))    {        rep(i,1,n)        {            scanf("%lf%lf%lf%lf",&a[i].first,&a[i].second,&b[i].first,&b[i].second);            if (a[i].first>b[i].first) swap(a[i],b[i]);            else if (a[i].first==b[i].first&&a[i].second>b[i].second) swap(a[i],b[i]);        }        int flag=0;        rep(i,1,n)        {            rep(j,i+1,n)            {                if (check(i,j)) {flag=1; break;}            }            if (flag) break;        }        puts(flag?"burned!":"ok!");    }    return 0;}

0 0
原创粉丝点击