http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1648
Circuit Board
Time 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!
解析:
题意:给出n条线段,判断是否存在线段相交
思路:
判断是否存在两线段相交,在于判断是否相互跨立
248 KB 120 ms C++ (g++ 4.4.5) 1112 B
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=2000+10;
struct point
{
double x;
double y;
}p1[maxn],p2[maxn];
double Cross(point A,point B)
{
return A.x*B.y-A.y*B.x;
}
point sub(point a,point b)
{
point c;
c.x=a.x-b.x;
c.y=a.y-b.y;
return c;
}
bool Intersect(point p1,point p2,point Q1,point Q2)
{ point v1=sub(p1,Q1);
point v2=sub(Q2,Q1);
point v3=sub(p2,Q1);
point v4=sub(p2,p1);
point v5=sub(Q2,p1);
point v6=sub(Q1,p1);
if(Cross(v1,v2)*Cross(v2,v3)>0&&Cross(v6,v4)*Cross(v4,v5)>0)//相互跨立,这里的相交不包含端点相交
return 1;
else
return 0;
}
int main()
{int n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&p1[i].x,&p1[i].y,&p2[i].x,&p2[i].y);
}
if(n==1)
{printf("ok!\n");
continue;
}
int ok=1;
for(i=0;i<n;i++)
{ if(!ok)
break;
for(j=i+1;j<n;j++)
{
if(Intersect(p1[i],p2[i],p1[j],p2[j]))
{ok=0;
break;
}
}
}
if(ok)
printf("ok!\n");
else
printf("burned!\n");
}
return 0;
}