標籤:des style blog http color io os ar for
歐拉定理:P+F-E=2
That Nice Euler Circuit
| Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld & %llu |
Submit Status
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 N4, 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
50 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 Status
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <vector>using namespace std;const double eps = 1e-8;int dcmp(double x){if(fabs(x)<eps) return 0; return (x<0)?-1:1;}struct Point{ double x,y; Point(double _x=0,double _y=0):x(_x),y(_y){};};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<(const Point&a,const Point&b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}bool operator==(const Point&a,const Point&b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}double Dot(Point A,Point B) {return A.x*B.x+A.y*B.y;}double Length(Point A) {return sqrt(Dot(A,A));}double Angle(Point A,Point B) {return acos(Dot(A,B)/Length(A)/Length(B));}double Angle(Point v) {return atan2(v.y,v.x);}double Cross(Point A,Point B) {return A.x*B.y-A.y*B.x;}/**Cross P*Q > 0 P在Q的順時針方向 P*Q < 0 P在Q的逆時針方向 P*Q = 0 PQ共線*/Point Horunit(Point x) {return x/Length(x);}///單位向量Point Verunit(Point x) {return Point(-x.y,x.x)/Length(x);}///單位法向量Point Rotate(Point A,double rad)///逆時針旋轉{ return Point(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}double Area2(const Point A,const Point B,const Point C){ return Cross(B-A,C-A);}/// 過兩點p1, p2的直線一般方程ax+by+c=0 (x2-x1)(y-y1) = (y2-y1)(x-x1)void getLineGeneralEquation(const Point& p1, const Point& p2, double& a, double&b, double &c){ a = p2.y-p1.y; b = p1.x-p2.x; c = -a*p1.x - b*p1.y;}///P+t*v Q+w*t的焦點Point GetLineIntersection(Point P,Point v,Point Q,Point w){ Point u=P-Q; double t=Cross(w,u)/Cross(v,w); return P+v*t;}///點到直線距離double DistanceToLine(Point P,Point A,Point B){ Point v1=B-A,v2=P-A; return fabs(Cross(v1,v2))/Length(v1);}///點到線段距離double DistanceToSegment(Point P,Point A,Point B){ if(A==B) return Length(P-A); Point v1=B-A,v2=P-A,v3=P-B; if(dcmp(Dot(v1,v2))<0) return Length(v2); else if(dcmp(Dot(v1,v3))>0) return Length(v3); else return fabs(Cross(v1,v2))/Length(v1);}///點到直線投影Point GetLineProjection(Point P,Point A,Point B){ Point v=B-A; return A+v*(Dot(v,P-A)/Dot(v,v));}///判斷規範相交bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){ double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1); double c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1); return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;}///一個點是否在直線端點上bool OnSegment(Point p,Point a1,Point a2){ return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;}///多邊形有向面積double PolygonArea(Point* p,int n){ double area=0; for(int i=1;i<n-1;i++) area+=Cross(p[i]-p[0],p[i+1]-p[0]); return area/2;}int n;Point pt[1000];vector<Point> vp;int main(){ int cas=1; while(scanf("%d",&n)!=EOF&&n) { vp.clear(); for(int i=0;i<n;i++) { scanf("%lf%lf",&pt[i].x,&pt[i].y); vp.push_back(pt[i]); } n--; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(SegmentProperIntersection(pt[i],pt[i+1],pt[j],pt[j+1])) { vp.push_back(GetLineIntersection(pt[i],pt[i+1]-pt[i],pt[j],pt[j+1]-pt[j])); } } } sort(vp.begin(),vp.end()); int c=unique(vp.begin(),vp.end())-vp.begin(); int e=n; int cc=0; for(vector<Point>::iterator it=vp.begin();cc<c&&it!=vp.end();cc++,it++) { for(int i=0;i<n;i++) { if(OnSegment(*it,pt[i],pt[i+1])) e++; } } printf("Case %d: There are %d pieces.\n",cas++,e+2-c); } return 0;}
UVALive 3263 That Nice Euler Circuit 計算幾何歐拉定理