That Nice Euler Circuit - POJ 2284 欧拉公式

39 篇文章 0 订阅

That Nice Euler Circuit
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 1790 Accepted: 560

Description

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 >= 4, which is the number of instructions in the test case. The following N pairs of integers give the instructions and 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 sample input cases. 

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.

题意:给你一个一笔画成的图,问它有多少个区域。

思路:欧拉公式 顶点-边数+面数=2,另外如果是有k个连通区域的话,那么就是 顶点-边数+面数=k+1。先找到所有的交点,再加上本来的点去重。对于一条边,如果它上面有m个点,那么就分成了m-1条边。所有边上的点次的总和-n+1就是边的数量。

           注意求交点的时候可能不能求到顶点的情况,所以需要加上本来的点。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps)
      return 0;
    return x<0 ? -1 : 1 ;
}
Point operator +(Point A,Point B){return Point(A.x+B.x,A.y+B.y);}
Point operator -(Point A,Point B){return Point(A.x-B.x,A.y-B.y);}
Point operator *(Point A,double p){return Point(A.x*p,A.y*p);}
Point operator /(Point A,double p){return Point(A.x/p,A.y/p);}
bool operator ==(Point A,Point B){return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;}
bool operator <(Point A,Point B)
{
    if(dcmp(A.x-B.x)==0)
      return A.y<B.y;
    return A.x<B.x;
}
double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;}
double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}
bool parallel(Point aa,Point ab,Point ba,Point bb){return dcmp(Cross(aa-ab,ba-bb))==0;}
bool jiaodian(Point aa,Point ab,Point ba,Point bb,Point &as)
{
    if(parallel(aa,ab,ba,bb))
      return 0;
    double s1=Cross(aa-ba,bb-ba);
    double s2=Cross(ab-ba,bb-ba);
    as=(ab*s1-aa*s2)/(s1-s2);
    return 1;
}
bool Onsegment(Point p,Point s,Point t){return dcmp(Cross(s-p,t-p))==0 && dcmp(Dot(s-p,t-p))<=0;}
Point p[510],temp,p2[100010];
int n,t,tot;
int main()
{
    int i,j,k,ret,ret2;
    while(~scanf("%d",&n) && n>0)
    {
        for(i=1;i<=n;i++)
           scanf("%lf%lf",&p[i].x,&p[i].y);
        tot=0;
        for(i=2;i<=n;i++)
           for(j=i+1;j<=n;j++)
              if(jiaodian(p[i],p[i-1],p[j],p[j-1],temp) && Onsegment(temp,p[i],p[i-1])&& Onsegment(temp,p[j],p[j-1]))
                p2[++tot]=temp;
        for(i=1;i<=n;i++)
           p2[++tot]=p[i];
        sort(p2+1,p2+1+tot);
        ret=1;
        for(i=2;i<=tot;i++)
           if(!(p2[i]==p2[i-1]))
             p2[++ret]=p2[i];
        tot=ret;
        ret2=0;
        for(i=2;i<=n;i++)
           for(j=1;j<=tot;j++)
              if(Onsegment(p2[j],p[i],p[i-1]))
                ret2++;
        printf("Case %d: There are %d pieces.\n",++t,2-tot+ret2-n+1);
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值