2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest F - Infinite Go

来源:互联网 时间:1970-01-01

Infinite GoTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 541 Accepted Submission(s): 157Problem Description Go is a proverbial board game originated in China. It has been proved to be the most difficult board game in the world. “The rules of Go are so elegant, organic, and rigorously logical that if intelligent life forms exist elsewhere in the universe, they almost certainly play Go.” said Emanuel Lasker, a famous chess master. A Go board consists of 19 horizontal lines and 19 vertical lines. So there are 361 cross points. At the beginning, all cross points are vacant. Go is played by two players. The basic rules are: 1. One player owns black stones and the other owns white stones. 2. Players place one of his stones on any vacant cross points of the board alternately. The player owns black stones moves first. 3. Vertically and horizontally adjacent stones of the same color form a chain. 4. The number of vacant points adjacent (vertically or horizontally) to a chain is called the liberty of this chain. Once the chain has no liberty, it will be captured and removed from the board. 5. While a player place a new stone such that its chain immediately has no liberty, this chain will be captured at once unless this action will also capture one or more enemy’s chains. In that case, the enemy’s chains are captured, and this chain is not captured. In effect, Go also has many advanced and complex rules. However, we only use these basic rules mentioned above in this problem. Now we are going to deal with another game which is quite similar to Go. We call it “Infinite Go”. The only difference is that the size of the board is no longer 19 times 19 -- it becomes infinite. The rows are numbered 1, 2, 3, ..., from top to down, and columns are numbered 1, 2, 3, ..., from left to right. Notice that the board has neither row 0 nor column 0, which means even though the board is infinite, it has boundaries on the top and on the left. In this problem, we are solving the problem that, given the actions of two players in a set of Infinite Go, find out the number of remaining stones of each player on the final board. Input The input begins with a line containing an integer T (1 <= T <= 20), the number of test cases. For each test case, the first line contains a single integer N (1 <= N <= 10000), the number of stones placed during this set. Then follows N lines, the i-th line contains two integer X and Y (1 <= X, Y <= 2,000,000,000), indicates that the i-th stone was put on row X and column Y (i starts from 1). The stones are given in chronological order, and it is obvious that odd-numbered stones are black and even-numbered ones are white. Output For each test case, output two integers Nb and Nw in one line, separated by a single space. Nb is the number of black stones left on the board, while Nw is the number of white stones left on the board. Sample Input175 54 53 53 44 43 34 6 Sample Output4 2 题目大意:

有个很大的围棋棋盘,最多10000颗棋子落下,问结束的时候黑白棋各有多少。

解题思路:

简单模拟,这题做了很久主要原因是真的有数据会自己把自己弄死。

#include "iostream"#include "vector"#include "map"#include "string.h"#include "cstdio"#include "queue"using namespace std;const int maxn=10005;typedef pair<int,int>pa;map<pa,int>step,vis;int sum[maxn],n,t,x[maxn],y[maxn],pnt[maxn];int go[2][4]={{0,-1,0,1},{-1,0,1,0}};int find(int x){return pnt[x]==x?x:pnt[x]=find(pnt[x]);}void Union(int x,int y){int a=find(x),b=find(y);if(a!=b){pnt[a]=b;sum[b]+=sum[a];}}void solve(int x,int y,int type){queue<pa>clean;clean.push(make_pair(x,y));vis.clear();vis[make_pair(x,y)]=1;while(!clean.empty()){pa now=clean.front();pnt[step[now]]=step[now]; //还原祖先sum[step[now]]=0;step.erase(now);clean.pop();for(int i=0;i<4;i++){int xx=now.first+go[0][i];int yy=now.second+go[1][i];if(xx<=0||yy<=0||vis[make_pair(xx,yy)])continue;if((step[make_pair(xx,yy)]&1)^type==0){vis[make_pair(xx,yy)]=1;clean.push(make_pair(xx,yy));}else{int temp=find(step[make_pair(xx,yy)]);sum[temp]++;}}}}int main(int argc, char* argv[]){scanf("%d",&t);while(t--){scanf("%d",&n);step.clear();for(int i=1;i<=n;i++){scanf("%d %d",&x[i],&y[i]);pnt[i]=i;sum[i]=0;}for(int i=1;i<=n;i++){step[make_pair(x[i],y[i])]=i;int num=0;for(int j=0;j<4;j++){int xx=x[i]+go[0][j];int yy=y[i]+go[1][j];if(xx<=0||yy<=0)continue;if(step.count(make_pair(xx,yy))==0){num++;continue;}int temp=find(step[make_pair(xx,yy)]);sum[temp]--;}sum[i]=num;for(int j=0;j<4;j++){int xx=x[i]+go[0][j];int yy=y[i]+go[1][j];if(xx<=0||yy<=0)continue;if(step.count(make_pair(xx,yy))==0)continue;if(((step[make_pair(xx,yy)]&1)^(i&1))==0)Union(i,step[make_pair(xx,yy)]);else{int temp=find(step[make_pair(xx,yy)]);if(sum[temp]==0) //把另个类型的棋子吃掉solve(xx,yy,1-(i&1));}}int temp=find(i);if(sum[temp]==0) //这傻子把自己的棋子吃掉了solve(x[i],y[i],i&1);}int sumw=0,sumb=0;vis.clear();for(int i=1;i<=n;i++){if(step.count(make_pair(x[i],y[i]))==0||vis[make_pair(x[i],y[i])])continue;vis[make_pair(x[i],y[i])]=1;if(step[make_pair(x[i],y[i])]&1)sumb++;elsesumw++;}printf("%d %d/n",sumb,sumw);}return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。



相关阅读:
Top