HDU -4775:Infinite Go

来源:互联网 发布:东方网络收购会成功吗 编辑:程序博客网 时间:2024/06/05 12:40

Infinite Go

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


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

思路:用map存点。每次某一个人下棋的位置为(x,y),就dfs检查其上下左右4个方向,如果是对手的棋且dfs的时候找不到空白格(即被围死了),就把dfs过的棋从map里删掉。注意:有可能会让自己被困死。

#include<cstring>#include<algorithm>#include<map>#include<cstdio>#include<iostream>using namespace std;const int maxn = 2e9+5;struct data{    int x,y;    bool friend operator<(const data &a,const data &b)    {        if(a.x == b.x)            return a.y < b.y;        else            return a.x < b.x;    }};int dir[4][2]={1,0,-1,0,0,1,0,-1};map<data,int> ma,mb;     //ma存黑棋的坐标,mb存白棋的坐标map<data,int>::iterator iter;map<data,int> vis;       //dfs的时候存下访问过的点map<data,int>::iterator it;int flag;   //用来表示棋子是否被困死(0表示被困死)void dfs1(int x,int y)   //对黑棋进行dfs{    int nx,ny;    vis[(data){x,y}] = 1;    for(int i = 0; i < 4; i++)    {        nx = x + dir[i][0];        ny = y + dir[i][1];        if(nx >= 1 && ny >= 1) //没有超过上界        {            if(ma.find((data){nx,ny}) == ma.end() && mb.find((data){nx,ny}) == mb.end()) //找到一个空白格,没有被困死            {                flag = 1;                return;            }            else if(ma.find((data){nx,ny}) != ma.end() && vis.find((data){nx,ny}) == vis.end())                dfs1(nx,ny);        }    }}void dfs2(int x,int y)       //对白棋进行dfs{    int nx,ny;    vis[(data){x,y}] = 1;    for(int i = 0; i < 4; i++)    {        nx = x + dir[i][0];        ny = y + dir[i][1];        if(nx >= 1 && ny >= 1)        {            if(ma.find((data){nx,ny}) == ma.end() && mb.find((data){nx,ny}) == mb.end()) //找到一个空白格            {                flag = 1;                return;            }            else if(mb.find((data){nx,ny}) != mb.end() && vis.find((data){nx,ny}) == vis.end())                dfs2(nx,ny);        }    }}int main(){    int T,n,x,y;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        ma.clear();        mb.clear();        vis.clear();        for(int i = 1; i <= n; i++)        {            scanf("%d%d",&x,&y);            if(i%2 == 1)ma[(data){x,y}] = 1;            else mb[(data){x,y}] = 2;           if(i%2)           {                for(int j = 0; j < 4; j++) //对周围的点进行dfs                {                    flag = 0;                    int nx = x + dir[j][0];                    int ny = y + dir[j][1];                    if(nx >= 1 && ny >= 1 && mb.find((data){nx,ny}) != mb.end())                    {                            vis.clear();                            dfs2(nx,ny);                            if(!flag)                            {                                for(it = vis.begin(); it != vis.end(); it++) //删掉被吃掉的点                                {                                    mb.erase(it->first);                                }                            }                    }                }                flag = 0;                vis.clear();                dfs1(x,y);//检查自己是否被困死                if(!flag)                {                    for(it = vis.begin(); it != vis.end(); it++)//删掉被吃掉的点                    {                        ma.erase(it->first);                    }                }            }            else            {                for(int j = 0; j < 4; j++)                {                    flag = 0;                    int nx = x + dir[j][0];                    int ny = y + dir[j][1];                    if(nx >= 1 && ny >= 1 && ma.find((data){nx,ny}) != ma.end())                    {                            vis.clear();                            dfs1(nx,ny);                            if(!flag)                            {                                for(it = vis.begin(); it != vis.end(); it++)//删掉被吃掉的点                                {                                    ma.erase(it->first);                                }                            }                    }                }                flag = 0;                vis.clear();                dfs2(x,y);//检查自己是否被困死                if(!flag)                {                    for(it = vis.begin(); it != vis.end(); it++)//删掉被吃掉的点                    {                        mb.erase(it->first);                    }                }            }        }        int a = 0,b= 0;        a = ma.size();        b = mb.size();        printf("%d %d\n",a,b);    }    return 0;}



原创粉丝点击