标签:cto problem 模拟 inf void ble 分享 printf fine
题目链接:hdu 4775 Infinite Go
题意:
给你n步下黑白子棋的过程,问你最后黑棋剩多少,白棋剩多少(围棋规则)
题解:
并查集模拟一下,用并查集记录一下链以及周围空白棋子的个数。
注意棋盘边上的旗子可能被围死。
1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=(a);i<=(b);++i) 3 using namespace std; 4 typedef long long ll; 5 typedef pair<int,int>P; 6 7 const int N=1e4+7; 8 map<P,P>mp; 9 set<int>Q; 10 int t,n,f[N],cnt[N],x,y,dir[][2]={1,0,-1,0,0,1,0,-1}; 11 12 int find(int x){return x==f[x]?x:f[x]=find(f[x]);} 13 void merge(int x,int y) 14 { 15 int fx=find(x),fy=find(y); 16 if(fx!=fy)cnt[fx]+=cnt[fy],f[fy]=fx,cnt[fy]=0; 17 } 18 19 void del(P x) 20 { 21 F(i,0,3) 22 { 23 int tx=x.first+dir[i][0],ty=x.second+dir[i][1]; 24 if(tx<1||ty<1)continue; 25 if(mp.find(P(tx,ty))!=mp.end()) 26 { 27 int fx=find(mp[P(tx,ty)].first); 28 cnt[fx]++; 29 } 30 } 31 } 32 vector<P>qq; 33 void delt(int fx) 34 { 35 qq.clear(); 36 for(auto it:mp) 37 { 38 if(find(it.second.first)==fx) 39 qq.push_back(it.first); 40 } 41 for(auto it:qq)mp.erase(it); 42 for(auto it:qq)del(it); 43 } 44 45 void put(int idx,int x,int y,int col) 46 { 47 P round[5]; 48 F(i,0,3)round[i]=P(0,0); 49 int ct=0; 50 F(i,0,3) 51 { 52 int tx=x+dir[i][0],ty=y+dir[i][1]; 53 if(tx<1||ty<1)continue; 54 if(mp.find(P(tx,ty))!=mp.end()) 55 { 56 round[i]=mp[P(tx,ty)]; 57 int fx=find(round[i].first); 58 cnt[fx]--; 59 Q.insert(fx); 60 }else ct++; 61 } 62 63 int fx=find(idx); 64 Q.insert(fx),cnt[fx]+=ct; 65 mp[P(x,y)]=P(idx,col); 66 F(i,0,3) 67 { 68 if(round[i].second==col) 69 merge(idx,round[i].first); 70 } 71 while(Q.size()) 72 { 73 int now=*(Q.begin());Q.erase(Q.begin()); 74 int fx=find(now); 75 if(cnt[fx]<=0)delt(fx); 76 } 77 } 78 79 int main(){ 80 scanf("%d",&t); 81 while(t--) 82 { 83 mp.clear(); 84 scanf("%d",&n); 85 F(i,1,n)f[i]=i,cnt[i]=0; 86 F(i,1,n) 87 { 88 scanf("%d%d",&x,&y); 89 put(i,x,y,(i&1)+1); 90 } 91 int a=0,b=0; 92 for(auto it:mp){ 93 if(it.second.second==2)a++; 94 else b++; 95 } 96 printf("%d %d\n",a,b); 97 98 } 99 return 0; 100 }
标签:cto problem 模拟 inf void ble 分享 printf fine
原文地址:http://www.cnblogs.com/bin-gege/p/7647714.html