您的位置:首页 > 产品设计 > UI/UE

ZOJ--1648:Circuit Board(跨立实验线段判交)

2017-06-19 13:07 323 查看
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条线段,我们都知道电路中的线路是不能相交的,所以一旦遇到相交的情况就输出burned!否则输出ok!

思路:判断两条线段是否相交

首先引入两个实验:

a.快速排斥实验(我没用这个 直接用的跨立实验,这个方法可以节省算法时间)

设以线段A1A2和线段B1B2为对角线的矩形为M,N;

若M,N 不相交,则两个线段显然不相交;

所以:满足第一个条件时:两个线段可能相交。

 

b.跨立实验

如果两线段相交,则两线段必然相互跨立对方.若A1A2跨立B1B2,则矢量( A1 - B1 ) 和(A2-B1)位于矢量(B2-B1)的两侧,



即(A1-B1) × (B2-B1) * (A2-B1) × (B2-B1)<0。(这里×表示叉乘)

上式可改写成(A1-B1) × (B2-B1) * (B2-B1) × (A2-A1)>0。

应该两条线段分别判断是否跨立,若是则两线段相交。

另外附上一个很牛逼的博客的判断线段相交问题:http://www.cnblogs.com/g0feng/archive/2012/05/18/2508293.html

AC代码:

import java.util.Scanner;

public class ZOJ_1648 {
static int n,flag;
static double arr[][]=new double[2000][4];
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
while(s.hasNext()){
n=s.nextInt();
for(int i=0;i<n;i++){
arr[i][0]=s.nextDouble();//x1
arr[i][1]=s.nextDouble();//y1
arr[i][2]=s.nextDouble();//x2
arr[i][3]=s.nextDouble();//y2
}
flag=0;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(((arr[j][0]-arr[i][0])*(arr[i][3]-arr[i][1])-(arr[j][1]-arr[i][1])*(arr[i][2]-arr[i][0]))*((arr[i][2]-arr[i][0])*(arr[j][3]-arr[i][1])-(arr[i][3]-arr[i][1])*(arr[j][2]-arr[i][0]))>0){
if(((arr[i][0]-arr[j][0])*(arr[j][3]-arr[j][1])-(arr[i][1]-arr[j][1])*(arr[j][2]-arr[j][0]))*((arr[j][2]-arr[j][0])*(arr[i][3]-arr[j][1])-(arr[j][3]-arr[j][1])*(arr[i][2]-arr[j][0]))>0){
flag=1;
break;
}
}
}
if(flag==1)
break;
}
if(flag==0)
System.out.println("ok!");
else
System.out.println("burned!");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: