HDU-4775---Infinite Go (bfs)

Infinite Go

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



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
  
  
1 7 5 5 4 5 3 5 3 4 4 4 3 3 4 6
 
Sample Output

4 2

题意:两个人下围棋,一个人执黑棋,一个人执白棋,两人轮流下,若一种颜色的棋子吧另一种颜色的棋子完全围住,不能有气,被围住的棋子被拿走,问最后两种棋子个剩下多少;
思路:每下一步棋,对这个点的四周进行bfs,然后对这个点bfs,由于地图无限大,存图以及标记都需要用map,对于每个坐标可以用pair存。
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<stack>
#include<set>
#include<algorithm>
#define LL long long
using namespace std;
map<pair<int,int>,int> mp,vis;//mp存坐标及颜色,vis标记
int n;
pair<int,int> que[10005],tmp;
int _next[4][2]={1,0,-1,0,0,1,0,-1};
void bfs(int x,int y,int type)//type即颜色
{
    vis.clear();
    int head=0,tail=-1;
    que[++tail]=make_pair(x,y);
    vis[make_pair(x,y)]=1;
    while(head<=tail)
    {
        tmp=que[head++];
        for(int i=0;i<4;i++)
        {
            int tx=tmp.first+_next[i][0];
            int ty=tmp.second+_next[i][1];
            if(tx>=1&&ty>=1&&vis[make_pair(tx,ty)]==0)
            {
                if(mp[make_pair(tx,ty)]==0)//当前点周围有气,直接return
                    return;
                else if(mp[make_pair(tx,ty)]==type)//与当前点的颜色相同入队
                    que[++tail]=make_pair(tx,ty);
                vis[make_pair(tx,ty)]=1;//标记
            }
        }
    }
    for(int i=0;i<=tail;i++)//若之前没有return,说明当前连通块被全部围住,则需要清空
        mp[que[i]]=0;//不用stl里面的queue就是在这清空时方便
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        mp.clear();
        scanf("%d",&n);
        int x,y;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            mp[make_pair(x,y)]=i&1?-1:1;//每个点的颜色
            for(int i=0;i<4;i++)
            {
                int tx=x+_next[i][0];
                int ty=y+_next[i][1];
                if(mp[make_pair(tx,ty)])
                    bfs(tx,ty,mp[make_pair(tx,ty)]);
            }
            bfs(x,y,i%1?-1:1);
        }
        int ans1=0,ans2=0;
        map<pair<int,int>,int>::iterator it;
        for(it=mp.begin();it!=mp.end();it++)//遍历mp,计算答案
        {
            if(it->second==-1) ans1++;
            if(it->second==1) ans2++;
        }
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值