您的位置:首页 > 其它

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!

。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/。/

模板题~~~

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: