首页 > 学院 > 开发设计 > 正文

uva1342 That Nice Euler Circuit

2019-11-06 09:18:34
字体:
来源:转载
供稿:网友

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 nally 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 in nite 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 ( X 0 ;Y 0) which moves the pencil to some starting position ( X 0 ;Y 0). 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 ( X 0 ;Y 0). In addition, the Euler machine will de nitely 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 rst pair is the rst 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 gures below illustrate the two sample input cases.

根据欧拉定理V+F−E=2,只需要算出点数V和边数E就可以知道面数F。求点数需要两两求交点然后去重,求边数需要判断点在线段上。

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;double eps=1e-8;struct vec{ double x,y; void rd() { scanf("%lf%lf",&x,&y); } bool Operator < (const vec v1) const { return (x+eps<v1.x)||(fabs(x-v1.x)<eps&&y+eps<v1.y); } bool operator == (vec v1) { vec self=(vec){x,y}; return !(self<v1)&&!(v1<self); } vec operator + (const vec &v1) const { return (vec){x+v1.x,y+v1.y}; } vec operator - (const vec &v1) const { return (vec){x-v1.x,y-v1.y}; } vec operator * (const double &k) const { return (vec){x*k,y*k}; } vec operator / (const double &k) const { return (vec){x/k,y/k}; }}a[100010],ver[100010];typedef vec point;int n,m;double dot(vec v1,vec v2){ return v1.x*v2.x+v1.y*v2.y;}double cross(vec v1,vec v2){ return v1.x*v2.y-v1.y*v2.x;}struct line{ point p; vec a;};point intersection(line l1,line l2){ vec u=l1.p-l2.p; double t=cross(l2.a,u)/cross(l1.a,l2.a); return l1.p+l1.a*t;}struct seg{ point a,b;};bool intersect(seg s1,seg s2){ double x1=cross(s2.b-s2.a,s1.a-s2.a),x2=cross(s2.b-s2.a,s1.b-s2.a), y1=cross(s1.b-s1.a,s2.a-s1.a),y2=cross(s1.b-s1.a,s2.b-s1.a); int ok1=int(x1<eps)+int(x2<eps),ok2=int(y1<eps)+int(y2<eps); return ok1==1&&ok2==1;}bool onseg(point p,seg s){ return fabs(cross(s.b-s.a,p-s.a))<eps&&dot(p-s.a,p-s.b)+eps<0;}int solve(){ int ans; n--; for (int i=1;i<=n+1;i++) a[i].rd(); m=n; for (int i=1;i<=n;i++) ver[i]=a[i]; for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) if (intersect((seg){a[i],a[i+1]},(seg){a[j],a[j+1]})) ver[++m]=intersection((line){a[i],a[i+1]-a[i]},(line){a[j],a[j+1]-a[j]}); sort(ver+1,ver+m+1); m=unique(ver+1,ver+m+1)-ver-1; ans=n-m+2; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (onseg(ver[j],(seg){a[i],a[i+1]})) ans++; return ans;}int main(){ int K=0; while (scanf("%d",&n)&&n) printf("Case %d: There are %d pieces./n",++K,solve());}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表