ZOJ--1648--计算几何--判断线段交点--没坑

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!

思路:这个题目没什么坑,参考模板即可:https://blog.csdn.net/queque_heiya/article/details/106187844

注意:数据类型用double;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath> 
using namespace std;
#define ll long long
const int maxa=2e3+10;
const double pi=1e-8;
const int inf=0x3f3f3f3f;
int n;
struct node{
	double x1,y1;
	double x2,y2;
}a[maxa];
bool check(node &a,node &b){
	if(!(min(a.x1,a.x2)<=max(b.x1,b.x2)
	&&min(b.y1,b.y2)<=max(a.y1,a.y2)
	&&min(b.x1,b.x2)<=max(a.x1,a.x2)
	&&min(a.y1,a.y2)<=max(b.y1,b.y2)))	return false;
	double u,v,w,z;
    u=(b.x1-a.x1)*(a.y2-a.y1)-(a.x2-a.x1)*(b.y1-a.y1);
    v=(b.x2-a.x1)*(a.y2-a.y1)-(a.x2-a.x1)*(b.y2-a.y1);
    w=(a.x1-b.x1)*(b.y2-b.y1)-(b.x2-b.x1)*(a.y1-b.y1);
    z=(a.x2-b.x1)*(b.y2-b.y1)-(b.x2-b.x1)*(a.y2-b.y1);
    return (u*v<=pi&&w*z<=pi);
}
int main(){
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++)
			scanf("%lf%lf%lf%lf",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
		int f=0;
		if(n==1)	{printf("ok!\n");continue;} 
		for(int i=0;i<n;i++){ 
			for(int j=0;j<n;j++){
				if(i!=j&&check(a[i],a[j])){
					printf("burned!\n");
					f=1;
					break;
				}
			}
			if(f)	break;
		}
		if(!f)	printf("ok!\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/queque_heiya/article/details/106222431
今日推荐