zoukankan      html  css  js  c++  java
  • 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!
    ");
     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!
    ");
    112         }
    113         else
    114         {
    115             printf("ok!
    ");
    116         }
    117     }
    118     return 0;
    119 }
  • 相关阅读:
    WebBrowser一点心得,如果在Javascript和Winform代码之间实现双向通信
    PHP
    ASP.NET的路由系统:根据路由规则生成URL
    数据库的范式模型
    P2P编程
    MS CRM 2011的自定义与开发(12)——表单脚本扩展开发(4)
    Html.RenderPartial和Html.Partial在Razor视图中的区别
    如何在本地安装 DotNetNuke 6
    ASP.NET的路由系统:路由映射
    ASP.NET Web API: 宿主(Hosting)
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3862200.html
Copyright © 2011-2022 走看看