链接1:http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=11851#problem/A
链接2:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1648
A - Circuit Board
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %lluSubmit Status Practice ZOJ 1648
Description
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!
解题思路:
判断是否存在两=条线段相交的情况。跨立实验:判断p1p2与Q1Q2是否相交则:((p1-Q1)×(Q2-Q1))*((Q2-Q1)×(p2-Q1)))>0&&((Q1-p1)×(p2-p1))*((p2-p1)×(Q2-p1)))>0
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; int n; struct Node {double x; double y; }p[2001][2]; int is_ok(Node p1,Node p2,Node Q1,Node Q2)//判断两条线段是否跨立 { double a1; double x1,x2,x3,y1,y2,y3; x1=p1.x-Q1.x;y1=p1.y-Q1.y; x2=Q2.x-Q1.x;y2=Q2.y-Q1.y; x3=p2.x-Q1.x;y3=p2.y-Q1.y; a1=(x1*y2-x2*y1)*(x2*y3-x3*y2); if(a1>1e-8)//因为是double 类型,所以要注意精度判断,不能直接与零比较 return 1; else return 0; } int main() { int i,j,ok; while(scanf("%d",&n)!=EOF) {for(i=0;i<n;i++) for(j=0;j<2;j++) scanf("%lf%lf",&p[i][j].x,&p[i][j].y); ok=1; if(n==1)//如果只有一条直线的特殊情况 ok=1; else { for(i=0;i<n;i++) {if(!ok)break; for(j=i+1;j<n;j++) if(is_ok(p[i][0],p[i][1],p[j][0],p[j][1])==1&&is_ok(p[j][0],p[j][1],p[i][0],p[i][1])==1) {ok=0;break;} } } if(ok) printf("ok!\n"); else printf("burned!\n"); } return 0; }