zoj 1648 判断线段是否相交

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=648

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!

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

模板题~~~

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <math.h>
  5 #include <iostream>
  6 #include <algorithm>
  7 #define eps 1e-6
  8
  9 struct point
 10 {
 11     double x,y;
 12 };
 13
 14 struct beline
 15 {
 16     point a,b;
 17 };
 18
 19 using namespace std;
 20
 21 point p[5000];
 22
 23 bool dy(double x,double y)
 24 {
 25     return x > y+eps;
 26 }
 27 bool xy(double x,double y)
 28 {
 29     return x < y-eps;
 30 }
 31 bool xyd(double x,double y)
 32 {
 33     return x < y+eps;
 34 }
 35 bool dyd(double x,double y)
 36 {
 37     return x > y-eps;
 38 }
 39 double dd(double x,double y)
 40 {
 41     return fabs(x-y) < eps;
 42 }
 43
 44 double crossProduct(point a,point b,point c)
 45 {
 46     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
 47 }
 48
 49 bool onSegment(point a,point b,point c)
 50 {
 51     double maxx=max(a.x,b.x);
 52     double maxy=max(a.y,b.y);
 53     double minx=min(a.x,b.x);
 54     double miny=min(a.y,b.y);
 55     if(dd(crossProduct(a,b,c),0.0)&&dyd(c.x,minx)&&xyd(c.x,maxx)
 56         &&dyd(c.y,miny)&&xyd(c.y,maxy))
 57         return true;
 58     return false;
 59 }
 60
 61 bool segIntersect(point p1,point p2,point p3,point p4)
 62 {
 63     double d1 = crossProduct(p3,p4,p1);
 64     double d2 = crossProduct(p3,p4,p2);
 65     double d3 = crossProduct(p1,p2,p3);
 66     double d4 = crossProduct(p1,p2,p4);
 67     if(xy(d1*d2,0.0)&&xy(d3*d4,0.0))
 68         return true;
 69     if(dd(d1,0.0)&&onSegment(p3,p4,p1))
 70         return true;
 71     if(dd(d2,0.0)&&onSegment(p3,p4,p2))
 72         return true;
 73     if(dd(d3,0.0)&&onSegment(p1,p2,p3))
 74         return true;
 75     if(dd(d4,0.0)&&onSegment(p1,p2,p4))
 76         return true;
 77     return false;
 78 }
 79
 80 int main()
 81 {
 82     beline p[5000];
 83     int n,i,j;
 84     while(scanf("%d",&n)!=EOF)
 85     {
 86         for(i=0;i<n;i++)
 87         {
 88                  scanf("%lf%lf%lf%lf",&p[i].a.x,&p[i].a.y,&p[i].b.x,&p[i].b.y);
 89         }
 90
 91         if(n<=1)
 92         {
 93             printf("ok!\n");
 94             continue;
 95         }
 96
 97         bool flag=false;
 98         for(i=0;i<n;i++)
 99         {
100             for(j=i+1;j<n;j++)
101             {
102                 if(segIntersect(p[i].a,p[i].b,p[j].a,p[j].b))
103                 {
104                     flag=true;
105                     break;
106                 }
107             }
108         }
109         if(flag)
110         {
111             printf("burned!\n");
112         }
113         else
114         {
115             printf("ok!\n");
116         }
117     }
118     return 0;
119 }

zoj 1648 判断线段是否相交

时间: 2024-08-07 17:00:45

zoj 1648 判断线段是否相交的相关文章

zoj 1010 Area 判断线段是否相交(把线段扩充一倍后 好处理) + 多边形求面积

题目来源: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 题意:  给定n个点的, 如果这n个点不能形成多边形 以及 n < 3 时, 输出, "Impossible",  否则 输出 多边形的面积. 分析: 这题主要在 分析  n 个点 是否形成 多边形.  枚举 每条边,  看 这条边 是否与 其他 n - 3 条边 不规范相交. (当处理 其他 边时, 我们采用 扩充线段一倍) 代码如下: con

Jack Straws(判断线段是否相交 + 并查集)

/** http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1840  题意: 判断线段是否相交  (包括间接相交) 输入: N(代表有n条线段) sx  sy  ex  ey(一条直线的两端点的坐标) : : : a b(判断第a条和第b条线段是否相交) : : : 0 0输入0 0 询问结束 输出:若相交输出  CONNECTED 否则         NOT CONNECTED */ #includ

POJ 1127 Jack Straws ( 求直线交点, 判断线段是否相交(含端点) )

题目:传送门 题意: 给你 n 条线段的两个端点, 然后有多次询问, 每次询问, 问你线段 x 和 线段 y 是否相交. 若线段 A 和线段 B 相交且线段 A 和线段 C 相交,那么线段 B 和线段 C 相交.     1 < n < 13 题解: 暴力求线段是否相交, 然后再跑个 Floyd 或者并查集都可以的. #include <iostream> #include <stdio.h> #include <string.h> #include <

codeForce-589D Boulevard(判断线段是否相交)

题目大意:n个人.一个区间.每个人都会在某个时间段内按相同的速度(所有人的速度都一样,都是1或-1)在他的区间内从一个端点走到另一个端点(只走一次).问每个人会与几个人碰面. 题目分析:将时间看成一个维度,区间位置看成另一个维度.那么每个人的状态便构成了一条二维线段.只需判断有几条线段与该线段相交. 判断两平面线段是否相交: 设线段1的两端点分别为A.B,线段2的两端点分别为C.D. 当两条线段平行时,只需要判断点C或点D是否在线段AB上即可.点C在线段AB上的判断:先判断向量CA与向量CB是否

hdu 1558 Segment set(并查集+判断线段是否相交)

代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int father[1005]; int son_cnt[1005]; char s[5]; //int cnt; struct point { double x,y; }; point a[1005],b[1005]; int find_father(int x) { int r=x; while(fathe

判断线段是否相交

struct Point{    int x1,y1,x2,y2;}A[20];int arr[20]; struct Node{    int x,y;};bool judge(Point p1,Point p2){   //相交最基本的条件    if(max(p1.x1,p1.x2)<min(p2.x1,p2.x2)||max(p1.y1,p1.y2)<min(p2.y1,p2.y2)||min(p1.x1,p1.x2)>max(p2.x1,p2.x2)||min(p1.y1,p1

hdoj-1086-You can Solve a Geometry Problem too 判断线段是否相交

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8683 Accepted Submission(s): 4227 Problem Description Many geometry(几何)problems were designed in the ACM/ICPC.

poj Pick-up sticks(判断线段相交)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11537   Accepted: 4337 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

POJ 2653 Pick-up sticks (判断线段相交)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10330   Accepted: 3833 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin