HDU 4775 Infinite Go 并查集

来源:互联网 发布:淘宝特卖网 编辑:程序博客网 时间:2024/06/06 03:36

题目链接:Infinite Go

Infinite Go

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 857    Accepted Submission(s): 279


Problem 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 Input
175 54 53 53 44 43 34 6
 

Sample Output
4 2
 

Source
2013 Asia Hangzhou Regional Contest 
 
题意:给出一个从(1,1)开始的无限大的棋盘,黑子白子轮流下棋,问按照围棋的规则最后双方剩多少棋。
题目分析:新加入点时需要并查集归类判断联通,同时统计每个集合周围一圈空格的数量,如果空格的数量变为0就要将这个集合删除,删除操作可以广搜遍历一遍,同时把对手棋子的集合加上空格,注意有可能出现自己下一步把自己堵死的情况,所以每次加点最后要对自身状态判断一次。
////  main.cpp//  HDU 4775 Infinite Go////  Created by teddywang on 2017/6/30.//  Copyright © 2017年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<queue>using namespace std;#define MP(x,y) make_pair(x,y)typedef pair<int,int> pir;int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};int T;int ans1,ans2;int num;int parent[100100],sum[100100],bx[100100],by[100100];map <pir,int> mp,vis;int find(int x){    if(x==parent[x]) return x;    else    {        parent[x]=find(parent[x]);        return parent[x];    }}void del(int x,int y,int c){    int nums=0;    queue<pir> q;    vis.clear();    vis[MP(x,y)]=1;    q.push(MP(x,y));    while(!q.empty())    {        pir buf=q.front();        nums++;        int fa=mp[buf];        parent[fa]=fa;        sum[fa]=0;        mp.erase(buf);        q.pop();        for(int i=0;i<4;i++)        {            int xx=buf.first+dx[i];            int yy=buf.second+dy[i];            if(xx<=0||yy<=0||vis[MP(xx,yy)]) continue;            int fb=mp[MP(xx,yy)];            if(((fb&1)^c)==0)            {                vis[MP(xx,yy)]=1;                q.push(MP(xx,yy));            }            else            {                int fs=find(fb);                sum[fs]++;            }        }    }    if(c==0) ans2-=nums;    else ans1-=nums;}int main(){    scanf("%d",&T);    while(T--)    {        scanf("%d",&num);        ans1=num/2+(num&1),ans2=num/2;        mp.clear();        for(int i=1;i<=num;i++)        {                        int x,y;            scanf("%d%d",&x,&y);            bx[i]=x;by[i]=y;            parent[i]=i;sum[i]=0;            mp[MP(x,y)]=i;            int empty=0;            for(int j=0;j<4;j++)            {                int xx=x+dx[j],yy=y+dy[j];                if(xx<=0||yy<=0) continue;                if(mp.count(MP(xx,yy))==0) empty++;                else                {                    int fa=find(mp[MP(xx,yy)]);                    sum[fa]--;                }            }            sum[i]=empty;            for(int j=0;j<4;j++)            {                int xx=x+dx[j],yy=y+dy[j];                if(xx<=0||yy<=0) continue;                if(mp.count(MP(xx,yy))==0) continue;                if(((i&1)^(mp[MP(xx,yy)]&1))==0)                {                    int fb=find(mp[MP(xx,yy)]);                    int t=find(i);                    if(fb!=t)                    {                        parent[t]=fb;                        sum[fb]+=sum[t];                    }                }                else                {                    int fb=find(mp[MP(xx,yy)]);                    if(sum[fb]==0)                        del(xx,yy,mp[MP(xx,yy)]&1);                }            }            int t=find(i);            if(sum[t]==0) del(x,y,i&1);        }        printf("%d %d\n",ans1,ans2);            }}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 下载东西呗安全管家制止了该怎么办 手机百度时不小心中毒扣话费怎么办 手机扫二维码中了木马病毒要怎么办 电脑管家微信扫描语音打不开怎么办 遇到花心老公又爱玩没有担当怎么办 软件全闪退返回键不管用了怎么办 为什么下载了我的世界打不开怎么办 问道手游安全锁忘记了怎么办 电脑显示网络电缆没有插好怎么办 手机扣扣的密码忘记了怎么办 扣扣忘记密码和密保怎么办 以前用的扣扣密码忘记了怎么办 我忘记扣扣支付密码了怎么办 百度云盘下载后怎么打不开怎么办 节奏大师领钻石卡丢了怎么办? 节奏大师体力赠送关了打不开怎么办 微信钱包转账转错账号怎么办 激活微信账号电话号码输错了怎么办 爱思助手加强版下载不了软件怎么办 苹果手机用爱思助手游戏闪退怎么办 新买的手机号支付宝被注册了怎么办 王者荣耀以前领的东西忘记换怎么办 科目三停车时把油门踩成刹车怎么办 澳邮奶粉快递过程中破了怎么办 酷狗音乐里删除歌曲时卡住了怎么办 6d卡槽弹簧坏了怎么办 微信违规被限制登录不可解封怎么办 手机卡号挂失后支付宝的钱怎么办 支付宝绑定的卡已经挂失怎么办 支付宝挂失了还有钱没还怎么办 qq号被盗时在是找不回来怎么办 被盗qq通过申诉找不回来怎么办 手机丢了微信的登陆密码忘了怎么办 手机丢了微信钱包有钱怎么办 华为手机微信应用锁密码忘记怎么办 手机丢了微信红包有钱怎么办 手机换号了微信密码忘记怎么办 电脑的宽带连接被删了怎么办 彩票中奖但是彩票老板打错了怎么办 支付宝交手机费没有到账怎么办 支付宝借充电宝丢了怎么办