1075 - Painter

发布时间:2016-12-9 14:02:12 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"1075 - Painter",主要涉及到1075 - Painter方面的内容,对于1075 - Painter感兴趣的同学可以参考一下。

You probably never heard of the painter Peer. He is not well known, much to his regret. Peer was one of the inventors of monochromy, which means that each of his paintings has a single color, but in different shades. He also believed in the use of simple geometric forms. During his triangle period, Peer drew triangles on a rectangular canvas, making sure their borders did not intersect. He would then choose a color, and fill the regions. Peer would paint the outermost region (the canvas itself) with the lightest shade of the color chosen. Then step by step, he would fill more inner regions with a darker shade of the same color. The image below is one of his ``Forms in Green" paintings. In a way the process was quite mechanical. The only thing Peer considered difficult was to decide, after drawing the triangles, how many different shades he would need. You must write a program to do that calculation for him. Your program will have a collection of triangles as its input. It should calculate the number of different shades needed to paint the regions according to the given rule. Your program must also detect the rare times that Peer makes a mistake and draws triangles that intersect. Two triangles are considered intersecting if the edges of one triangle have at least one point in common with the edges of the other. In that case, the collection of triangles is invalid. Input  The input file contains multiple test cases. The first line of each test case contains a single non-negative integer n (n100000) , which is the number of triangles in the test case. The following n lines of the test case contain the descriptions of triangles in the format x1 y1 x2 y2 x3 y3 , where xi , yi are integers (- 100000 < xi, yi < 100000) that are the coordinates of the vertices of the triangles. The three points are guaranteed not to be collinear. The last test case is followed by `-1' on a line by itself. Output  For each test case, print the case number (beginning with 1) and the number of shades needed to fill the regions if the test case is valid. Print the word `ERROR' if the test case is invalid (two or more triangles in the test case intersect). Sample Input  8 8 3 8 4 7 4 14 13 -1 9 9 0 1 8 7 7 4 10 5 10 11 8 13 12 9 10 11 10 11 9 2 7 9 1 10 6 5 5 5 6 8 6 9 2 9 5 6 4 2 0 0 1 0 0 1 2 0 1 1 1 -1 -1 Sample Output  Case 1: 5 shades Case 2: ERROR #include<stdio.h> #include<time.h> #include<string.h> #include<utility> #include<algorithm> #include<set> #include<math.h> #include<stdlib.h> #include<iostream> #define X first #define Y second using namespace std; const int MAXN=300110; const int MAXM=900110; const long double PI=3.1415926535898; const long double EPS=1e-9; typedef pair<long double,long double> Tpoint; Tpoint a[MAXM][3]; int n; pair<int,int> b[MAXM]; int f[MAXN],d[MAXN]; long double tmp; struct Tnode { long double p,v; int t,pos; inline bool operator==(const Tnode &x) { return (x.t==t) && (x.pos==pos); } inline long double value() { return (p+v*tmp); } }; inline bool operator < (const Tnode &y,const Tnode &x) { return (y.p+y.v*tmp<x.p+x.v*tmp-EPS)||(((fabs(y.p+y.v*tmp-x.p-x.v*tmp)<EPS)&&(y.t<x.t))); } inline bool equal(long double x,long double y) { return fabs(x-y)<EPS; } int cmp(const void *a,const void *b) { return (*(long double *)a > *(long double *)b ? 1 : -1); } inline bool cmp2(const pair<int,int>&x,const pair<int,int>&y) { return a[x.X][x.Y].X<a[y.X][y.Y].X; } inline void rorate(Tpoint &a,long double alpha) { long double x,y; x=a.X*cos(alpha) - a.Y*sin(alpha); y=a.X*sin(alpha) + a.Y*cos(alpha); a.X=x; a.Y=y; } int getf(int p) { if(p==-1) return 1; else if(d[p]!=-1) return d[p]; else return d[p]=getf(f[p])+1; } void init() { int i,j,k,l; int tx,ty; for(i=0;i<n;++i) for(j=0;j<3;++j) { scanf("%d %d",&tx,&ty); a[i][j].X=tx; a[i][j].Y=ty; } long double alpha=rand()%30000/30000.0*PI; for(i=0;i<n;++i) { for(j=0;j<3;++j) rorate(a[i][j],alpha); qsort(a[i],3,sizeof(long double)*2,cmp); b[3*i].X=b[3*i+1].X=b[3*i+2].X=i; b[3*i].Y=0; b[3*i+1].Y=1; b[3*i+2].Y=2; } sort(b,b+3*n,cmp2); } void solve() { int i,j,k,l; Tnode t1,t2,t3; long double td; set<Tnode> t; set<Tnode> :: iterator i1,i2,i3; t.clear(); bool ok=1; memset(f,0xff,sizeof(f)); for(i=0;ok&&(i<3*n);++i) { j=b[i].X; k=b[i].Y; tmp=a[j][k].X; if(k==0) { t1.v=(a[j][1].Y - a[j][0].Y)/(a[j][1].X - a[j][0].X); t1.p=a[j][0].Y-a[j][0].X*t1.v; t2.v=(a[j][2].Y - a[j][0].Y)/(a[j][2].X - a[j][0].X); t2.p=a[j][0].Y-a[j][0].X*t2.v; t1.pos=t2.pos=j; if(t1.v>t2.v) { t3=t1; t1=t2; t2=t3; } t1.t=0; t2.t=1; i1=t.lower_bound(t1); t3=(*i1); if((i1!=t.end())&&equal(t3.value(),t1.value())) { ok=false; break; } t.insert(t1); t.insert(t2); i1=t.find(t1); if(i1==t.end()) { ok=false; break; } if(i1!=t.begin()) { i1--; if(i1->t==1) f[j]=f[i1->pos]; else f[j]=i1->pos; } } else if(k==1) { if((a[j][1].Y-a[j][0].Y)/(a[j][1].X-a[j][0].X)<(a[j][2].Y-a[j][0].Y)/(a[j][2].X-a[j][0].X)) l=0; else l=1; t1.v=(a[j][1].Y - a[j][0].Y)/(a[j][1].X - a[j][0].X); t1.p=a[j][0].Y-a[j][0].X*t1.v; t1.pos=j; t1.t=l; td=t1.value(); i1=t.find(t1); if(i1==t.end()) { ok=false; break; } if(i1!=t.begin()) { i1--; t3=(*i1); if(td<=t3.value()+EPS) { ok=false; break; } i1++; } i1++; if(i1!=t.end()) { t3=(*i1); if(td>=t3.value()-EPS) { ok=false; break; } } t.erase(t1); t1.v=(a[j][2].Y - a[j][1].Y)/(a[j][2].X - a[j][1].X); t1.p=a[j][1].Y-a[j][1].X*t1.v; t.insert(t1); } else { if((a[j][1].Y-a[j][0].Y)/(a[j][1].X-a[j][0].X)>=(a[j][2].Y-a[j][0].Y)/(a[j][2].X-a[j][0].X)) { t1.v=(a[j][2].Y - a[j][0].Y)/(a[j][2].X - a[j][0].X); t1.p=a[j][0].Y-a[j][0].X*t1.v; } else { t1.v=(a[j][2].Y - a[j][1].Y)/(a[j][2].X - a[j][1].X); t1.p=a[j][1].Y-a[j][1].X*t1.v; } t1.pos=j; t1.t=0; i2=t.find(t1); i1=i2++; if((i1==t.end())||(i2==t.end())||(i2->pos!=j)||(i2->t!=1)) { ok=false; break; } td=t1.value(); i3=i2++; t.erase(i3); if(i2!=t.end()) { t3=(*i2); if(t3.value()<=EPS+td) { ok=false; break; } } i3=i1--; if(i3!=t.begin()) { t3=(*i1); if(t3.value()>=td-EPS) { ok=false; break; } } t.erase(i3); } } if(!ok) { printf("ERROR\n"); return; } int ans=1; memset(d,0xff,sizeof(d)); for(i=0;i<n;++i) ans=max(ans,getf(i)); printf("%d shades\n",ans); } int main() { srand(time(NULL)); int CASE=0; while((scanf("%d",&n)!=EOF)&&(n!=-1)) { init(); printf("Case %d: ",++CASE); solve(); } return 0; }

上一篇:SIFT算法:特征描述子
下一篇:企业级APP 行业发展的“下一个金矿”

相关文章

关键词: 1075 - Painter

相关评论