That Nice Euler Circuit
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 1975 | Accepted: 624 |
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.
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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/140b2c9ab169b758446903004544970a.jpeg)
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.
Source
[Submit] [Go Back] [Status] [Discuss]
————————————————————我是分割线————————————————————————
绝世好题。
大体思路简单,细节能烦死人。
改了2天,看着题解才写出几个函数。55555........
本题根据平面图的欧拉定理求解区域个数r。
顶点个数:两两线段求交点,每个交点都是图中的顶点。
边数:在求交点时判断每个交点落在几条边上,若一个交点落在某条边上,则将这条边分裂成两条边,边数++。
之后运用欧拉定理求区域个数即可。
最后减的时候,把n处理成了点数,错的离谱......
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
1 /* 2 Problem:poj 2284 3 OJ: POJ 4 User: S.B.S. 5 Time: 454 ms 6 Memory: 3548 kb 7 Length: 2643 b 8 */ 9 #include<iostream> 10 #include<cstdio> 11 #include<cstring> 12 #include<cmath> 13 #include<algorithm> 14 #include<queue> 15 #include<cstdlib> 16 #include<iomanip> 17 #include<cassert> 18 #include<climits> 19 #include<vector> 20 #include<list> 21 #include<map> 22 #define maxn 90001 23 #define F(i,j,k) for(int i=j;i<k;i++) 24 #define M(a,b) memset(a,b,sizeof(a)) 25 #define FF(i,j,k) for(int i=j;i>=k;i--) 26 #define inf 0x7fffffff 27 #define maxm 2016 28 #define mod 1000000007 29 #define eps 1e-10 30 //#define LOCAL 31 using namespace std; 32 int read(){ 33 int x=0,f=1;char ch=getchar(); 34 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 35 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 36 return x*f; 37 } 38 int n,m; 39 struct POINT //点 40 { 41 double x; 42 double y; 43 POINT(double a=0,double b=0){x=a;y=b;} 44 }; 45 struct EDGE //线段 46 { 47 POINT u; 48 POINT v; 49 EDGE(POINT A,POINT B) {u=A;v=B;} 50 }; 51 struct LINE //直线 52 { 53 double a; 54 double b; 55 double c; 56 }; 57 bool operator < (POINT A,POINT B) 58 { 59 return A.x<B.x || A.x==B.x && A.y<B.y; 60 } 61 bool operator == (POINT a,POINT b) 62 { 63 return abs(a.x-b.x)<eps && abs(a.y-b.y)<eps; 64 } 65 bool online(EDGE a,POINT b) 66 { 67 return abs((a.v.x-a.u.x)*(b.y-a.u.y)-(b.x-a.u.x)*(a.v.y-a.u.y))<eps && (b.x-a.u.x)*(b.x-a.v.x)<eps && (b.y-a.u.y)*(b.y-a.v.y)<eps; 68 } 69 LINE makeline(POINT a,POINT b) 70 {//将线段延长成直线 71 LINE l; 72 l.a=(b.y>a.y) ? b.y-a.y : a.y-b.y; //y方向差值 73 l.b=(b.y>a.y) ? a.x-b.x : b.x-a.x; //x方向差值 74 l.c=(b.y>a.y) ? a.y*b.x-a.x*b.y : a.x*b.y-a.y*b.x;//与水平线夹角 75 return l; //返回直线 76 } 77 bool lcross(LINE a,LINE b,POINT &p) 78 {//判断直线是否相交 79 double d=a.a*b.b-b.a*a.b; 80 if(abs(d)<eps) return false; 81 //求交点 82 p.x=(b.c*a.b-a.c*b.b)/d; 83 p.y=(b.a*a.c-a.a*b.c)/d; 84 return true; 85 } 86 bool ecross(EDGE a,EDGE b,POINT &p) 87 { 88 LINE l1,l2; 89 l1=makeline(a.u,a.v); 90 l2=makeline(b.u,b.v); 91 if(lcross(l1,l2,p)) return online(a,p) && online(b,p); 92 else return false; 93 } 94 POINT p[maxn],in[maxn]; 95 int cur,cnt,ans; 96 int main() 97 { 98 std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 99 #ifdef LOCAL 100 freopen("data.in","r",stdin); 101 freopen("data.out","w",stdout); 102 #endif 103 int t=0; 104 while(cin>>n&&n) 105 { 106 t++;m=cur=0; 107 for(int i=0;i<n;i++) cin>>p[i].x>>p[i].y; 108 for(int i=0;i<n;i++) 109 for(int j=0;j<n;j++){ 110 EDGE l1(p[i],p[(i+1)%n]),l2(p[j],p[(j+1)%n]); 111 POINT pp; 112 if(ecross(l1,l2,pp)) in[cur++]=pp; 113 } 114 sort(in,in+cur); 115 cur=unique(in,in+cur)-in; 116 for(int i=0;i<cur;i++) 117 for(int j=0;j<n;j++){ 118 EDGE ll(p[j],p[(j+1)%n]); 119 if(online(ll,in[i]) && !(ll.u==in[i])) m++; 120 } 121 cout<<"Case "<<t<<": There are "<<2+m-cur<<" pieces."<<endl; 122 } 123 return 0; 124 }