标签:hdu 4056 draw a mess 数据结构-并查集
8 8 4 Diamond 3 3 1 1 Triangle 4 4 3 2 Rectangle 1 1 2 2 3 Circle 6 6 2 4
4 4 4 11 0 0 0 0 0HintThe final distribution of different colors: 00000000 03300000 03310000 00111000 00022240 00002444 00004444 00000444
#include <string> #include <cmath> #include <iostream> #include <cstdio> #include <vector> using namespace std; const double eps = 1e-8; const int maxn = 210; const int maxm = 50010; int m , n , q , color[15] , father[maxm] , vis[maxm]; struct Node{ char op; int xc , yc , r , l , w,c; Node(char OP = 'x' , int XC = 0, int YC = 0, int R = 0, int L = 0 , int W = 0, int C = 0){ op = OP , xc = XC , yc = YC , l = L , r = R , w = W , c = C; } }; vector<Node> v; int findFather(int x){ if(father[x] != x) return father[x] = findFather(father[x]); return father[x]; } inline bool getCircle(int pos,int xc,int yc,int r,int &L,int &R) { int tmp = r*r-(pos-xc)*(pos-xc); if(tmp<0) return false; L = int(-sqrt(tmp*1.0)+yc+1.0-eps); //L = max(0,L); if(0 > L) L = 0; R = int(sqrt(tmp*1.0)+yc); //R = min(m-1,R); if(R > m-1) R = m-1; if(L>R) return false; return true; } inline bool getRect(int pos,int xc,int yc,int l,int w,int &L,int &R) { if(pos<xc || pos >= xc+l) return false; else { L = yc; //L = max(0,L); if(0 > L) L = 0; R = yc+w-1; //R = min(m-1,R); if(R > m-1) R = m-1; if(L>R) return false; return true; } } inline bool getDiamond(int pos,int xc,int yc,int r , int &L , int &R) { int tmp = r - abs(xc-pos); if(tmp < 0) return false; L = yc-tmp; //L = max(0,L); if(0 > L) L = 0; R = yc+tmp; //R = min(m-1,R); if(R > m-1) R = m-1; if(L>R) return false; return true; } inline bool getTriangle(int pos , int xc , int yc , int w , int &L , int &R){ if(pos < xc) return false; L = yc-w/2+pos-xc; R = yc+w/2-(pos-xc); //L = max(L , 0); if(0 > L) L = 0; //R = min(R , m-1); if(R > m-1) R = m-1; if(L>R) return false; return true; } void initial(){ for(int i = 0; i < 15; i++) color[i] = 0; v.clear(); for(int i = 0; i < maxm; i++) father[i] = i , vis[i] = 0; } void computing(){ char shape[20]; int xc , yc , w , r , c , l , L , R; while(q--){ scanf("%s" , shape); if(shape[0] == 'C'){ scanf("%d%d%d%d" , &xc , &yc , &r , &c); } if(shape[0] == 'D'){ scanf("%d%d%d%d" , &xc , &yc , &r , &c); } if(shape[0] == 'R'){ scanf("%d%d%d%d%d" ,&xc ,&yc ,&l ,&w ,&c); } if(shape[0] == 'T'){ scanf("%d%d%d%d" ,&xc,&yc,&w,&c); } v.push_back(Node(shape[0] , xc , yc , r , l , w , c)); } int fl , fr; for(int i = 0; i < n; i++){ //cout << i << ": "; for(int j = 0; j < m; j++) father[j] = j , vis[j] = 0; for(int k = v.size()-1; k >= 0; k--){ if(v[k].op == 'C' && !getCircle(i , v[k].xc , v[k].yc , v[k].r , L , R))continue; if(v[k].op == 'D' && !getDiamond(i , v[k].xc , v[k].yc , v[k].r , L , R)) continue; if(v[k].op == 'R' && !getRect(i , v[k].xc , v[k].yc , v[k].l , v[k].w , L , R)) continue; if(v[k].op == 'T' && !getTriangle(i , v[k].xc , v[k].yc , v[k].w , L , R)) continue; c = v[k].c; //cout << L << " " << R << " " << c << endl; int fl = findFather(L); while(R>=L){ if(vis[R] == 0){ vis[R] = 1; color[c]++; } fr = findFather(R); R = min(fr-1 , R-1); if(fr > fl)father[fr] = fl; } } } printf("%d" , color[1]); for(int i = 2; i <= 9; i++) printf(" %d" , color[i]); printf("\n"); } int main(){ while(~scanf("%d%d%d" , &n , &m , &q)){ initial(); computing(); } return 0; }
hdu 4056 Draw a Mess(数据结构-并查集)
标签:hdu 4056 draw a mess 数据结构-并查集
原文地址:http://blog.csdn.net/u011836218/article/details/39403047