• LA 3263 That Nice Euler Circuit(欧拉定理)


    That Nice Euler Circuit

     

    Little Joey invented a scrabble

    machine that he called Euler, after the great mathematician. In his primary school Joey heard about the nice story of how Euler started the study about graphs. The problem in that story was - let me remind you - to draw a graph on a paper without lifting your pen, and finally return to the original position. Euler proved that you could do this if and only if the (planar) graph you created has the following two properties: (1) The graph is connected; and (2) Every vertex in the graph has even degree.

     

    Joey's Euler machine works exactly like this. The device consists of a pencil touching the paper, and a control center issuing a sequence of instructions. The paper can be viewed as the infinite two-

    dimensional plane; that means you do not need to worry about if the pencil will ever go off the boundary.

    In the beginning, the Euler machine will issue an instruction of the form (X0, Y0) which moves the pencil to some starting position (X0, Y0). Each subsequent instruction is also of the form (X', Y'), which means to move the pencil from the previous position to the new position (X', Y'), thus draw a line segment on the paper. You can be sure that the new position is different from the previous position for each instruction. At last, the Euler machine will always issue an instruction that move the pencil back to the starting position(X0, Y0). In addition, the Euler machine will definitely not draw any lines that overlay other lines already drawn. However, the lines may intersect.

    After all the instructions are issued, there will be a nice picture on Joey's paper. You see, since the pencil is never lifted from the paper, the picture can be viewed as an Euler circuit.

    Your job is to count how many pieces (connected areas) are created on the paper by those lines drawn by Euler.

     

    Input 

    There are no more than 25 test cases. Ease case starts with a line containing an integer N$ ge$4, which is the number of instructions in the test case. The following N pairs of integers give the instructions a

    nd appear on a single line separated by single spaces. The first pair is the first instruction that gives the coordinates of the starting position. You may assume there are no more than 300 instructions in each test case, and all the integer coordinates are in the range (-300, 300). The input is terminated when N is 0.

     

    Output 

    For each test case there will be one output line in the format

     

    Case x: There are w pieces.,

     

    where x is the serial number starting from 1.

     

    Note: The figures below illustrate the two sa

    mple input cases.

     

    epsfbox{p3263.eps}

     

    Sample Input 

     

    5
    0 0 0 1 1 1 1 0 0 0 
    7 
    1 1 1 5 2 1 2 5 5 1 3 5 1 1 
    0
    

     

    Sample Output 

     

    Case 1: There are 2 pieces. 
    Case 2: There are 5 pieces.



      1 本题想到了还是不难!!
      2 
      3 
      4 首先显然要用欧拉定理  V+F-E=2;从而F=E+2-C;
      5 
      6 点个数=原来的+新增的;(可能三条线段交于一点,故新增的点要去重)
      7 
      8 然后在每一条线段上若新增一个点边就+1!!!
      9 
     10 
     11 
     12 
     13 
     14 
     15 #include<stdio.h>
     16 #include<math.h>
     17 #include<algorithm>
     18 #include<set>
     19 using namespace std;
     20 #define eps 1e-8
     21 #define oo 100000000
     22 #define pi acos(-1) 
     23 struct point
     24 {
     25     double x,y;
     26      point(double _x = 0.0,double _y = 0.0)
     27     {
     28         x =_x;
     29         y =_y;
     30     }
     31     point operator -(const point &b)const
     32     {
     33         return point(x - b.x, y - b.y);
     34     }
     35     point operator +(const point &b)const
     36     {
     37         return point(x +b.x, y + b.y);
     38     }
     39     double operator ^(const point &b)const
     40     {
     41         return x*b.y - y*b.x;
     42     }
     43     double operator *(const point &b)const
     44     {
     45         return x*b.x + y*b.y;
     46     }
     47     void input()
     48     {
     49         scanf("%lf%lf",&x,&y);
     50     }
     51 };
     52 
     53 int dcmp(double a)//判断一个double型的符号 
     54 {
     55     if(fabs(a)<eps)return 0;
     56     if(a>0)return 1;
     57     else return -1; 
     58 }
     59 
     60  point operator |(point a,double p)//重载数乘,向量*数!!! 
     61  {
     62      return point(a.x*p,a.y*p);
     63  }
     64     
     65 bool operator ==(const point &a,const point &b)
     66 {
     67     return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; 
     68 }
     69 
     70 bool SegmentGuiFanXiangJiao(point a1,point a2,point b1,point b2)
     71 {
     72     double c1=(a2-a1)^(b1-a1),c2=(a2-a1)^(b2-a1),
     73            c3=(b2-b1)^(a1-b1),c4=(b2-b1)^(a2-b1);
     74      return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
     75 }
     76 
     77 point getjiaodian(point p,point v,point q,point w)//前提有唯一交点!!参数方程,v,w都为方向向量,p,q,为两直线上的点,求交点 !
     78 {
     79     point u;
     80     u=p-q;
     81     double t=(w^u)/(v^w);
     82     v.x=t*v.x;v.y=t*v.y;
     83     return p+v;
     84 }
     85 
     86 bool cmp(point a,point b)
     87 {
     88     if(dcmp(a.x-b.x)==0)return a.y<b.y;
     89     return a.x<b.x;
     90 }
     91 bool OnSegment(point p,point a1,point a2)//不规范相交,点在另一条线段上 
     92 {
     93     return dcmp((a1-p)^(a2-p))==0&&dcmp((a1-p)*(a2-p))<0;
     94 }
     95 
     96 point p[400],V[100000];
     97 int main()
     98 {
     99     int i,j,n,ca;
    100     ca=0; 
    101     while(~scanf("%d",&n)&&n)
    102     {
    103         ca++;
    104         int c=0;
    105         for(i=0;i<n;i++)p[i].input(),V[c++]=p[i];
    106         n--;
    107         int e=n;
    108         for(i=0;i<n;i++)
    109         for(j=i+1;j<n;j++)
    110         if(SegmentGuiFanXiangJiao(p[i],p[i+1],p[j],p[j+1]))
    111             V[c++]=getjiaodian(p[i],p[i+1]-p[i],p[j],p[j+1]-p[j]);
    112         sort(V,V+c,cmp);//cmp必须要有,否则编译就bug啦!! 
    113         c=unique(V,V+c)-V;
    114         for(i=0;i<c;i++)
    115         for(j=0;j<n;j++)
    116         if(OnSegment(V[i],p[j],p[j+1]))e++;
    117         printf("Case %d: There are %d pieces.
    ",ca,e+2-c);                 
    118     }    
    119     return 0;
    120 }
    View Code
  • 相关阅读:
    21-while里的break简单用法
    20-使用while循环求从1累加至100的值
    19-random猜数
    18-random猜数,直到正确。
    17-简化后的石头剪刀布
    16-if实现石头剪刀布
    生成随机数
    转换数字的进制(Integer、Long)
    数字的舍入
    格式化数字
  • 原文地址:https://www.cnblogs.com/skykill/p/3236204.html
Copyright © 2020-2023  润新知