首页 > ACM题库 > HDU-杭电 > HDU 4158-GO-搜索-[解题报告]HOJ
2015
05-23

HDU 4158-GO-搜索-[解题报告]HOJ

GO

问题描述 :

In the game of Go, two players alternate placing black and white stones on lattice points of an n × n grid, each attempting to surround as much territory (i.e., regions of unfilled lattice points) as possible. At the end of the game, the score for each player is the total area of the territory surrounded by his or her stones. Given the locations of black and white stones on a Go board at the end of a match, your task is to compute the score of each player in order to determine the winner.1
Formally, two grid lattice points with coordinates (r, c) and (r′, c′) are adjacent if |r – r′| + |c – c′| = 1. A connected region of unfilled lattice points belongs to one player’s territory if all adjacent filled lattice points contain stones belonging to that player (see Figure 1). Finally, a player’s score consists of the number of unfilled lattice points in his or her territory.
Slalom

Note that the scoring of Go boards described here does not correspond exactly to the real game of Go: we make the simplifying assumptions that all “disputes” have been settled so that any territories surrounded by stones of both colors are considered neutral, and that all groups on the board are considered “alive.”

Figure 1: Diagram of a 9 × 9 Go board. Unfilled lattice points belonging to black’s territory are marked with B, and unfilled lattice points belonging to white’s territory are marked with W. Neutral unfilled lattice points are unmarked. In the game above, white wins by 21-3 = 18.

输入:

The input test file will contain multiple cases, each consisting of three lines. Each test case begins with a line containing three integers, n (where 1 ≤ n ≤ 19), b, and w (where b ≥ 0,w ≥ 0 and 1 ≤ b + w ≤ n2). Here, n denotes the size of the board, b is the number of black pieces placed, and w is the number of white pieces placed. The second line of each test case contains b pairs of integers r1 c1 . . . rb cb (where 1 ≤ ri , ci ≤ n) indicating the positions of the b black stones. The third line of each test case contains w pairs of integers r`1 c`1 . . . r`w c`w (where 1 ≤ r`i , c`i ≤ n) indicating the positions of the w white stones. No two stones will be located at the same lattice point. Input is terminated by a single line containing only the number 0; do not process this line.

输出:

The input test file will contain multiple cases, each consisting of three lines. Each test case begins with a line containing three integers, n (where 1 ≤ n ≤ 19), b, and w (where b ≥ 0,w ≥ 0 and 1 ≤ b + w ≤ n2). Here, n denotes the size of the board, b is the number of black pieces placed, and w is the number of white pieces placed. The second line of each test case contains b pairs of integers r1 c1 . . . rb cb (where 1 ≤ ri , ci ≤ n) indicating the positions of the b black stones. The third line of each test case contains w pairs of integers r`1 c`1 . . . r`w c`w (where 1 ≤ r`i , c`i ≤ n) indicating the positions of the w white stones. No two stones will be located at the same lattice point. Input is terminated by a single line containing only the number 0; do not process this line.

样例输入:

1 1 0
1 1
2 0 1
1 1
5 12 4
1 1 1 2 1 3 2 1 2 3 3 1 3 3 4 1 4 3 5 1 5 2 5 3
1 4 2 4 3 4 3 5 
0

样例输出:

Draw
White wins by 3
Black wins by 1

/*搜索中状态加入队列时就要立即更新标志,

不能出队的时候更新,否则中间有部分状态会重复搜索造成错误。*/

#include <stdio.h>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
int map[21][21];
bool vis[21][21];
int dx[4]= {1,-1,0,0};
int dy[4]= {0,0,-1,1};
int cb,cw,n;
struct point
{
    int x,y;
}p,t;
void bfs(int x,int y)
{
    p.x=x,p.y=y;
    queue <point> q;
    q.push(p);
    int num=0;
    vis[p.x][p.y]=true;
    int r2=-1,r3=-1;
    while(!q.empty())
    {
        t=q.front();
        num++;
        q.pop();
        for(int i=0; i<4; i++)
        {
            p.x=t.x+dx[i];
            p.y=t.y+dy[i];
            if(vis[p.x][p.y]) continue;
            if(p.x<1||p.x>n||p.y<1||p.y>n) continue;
            if(map[p.x][p.y]==1) r2=1;
            else if(map[p.x][p.y]==2) r3=1;
            else q.push(p),vis[p.x][p.y]=true;
        }
    }
    if(r2==1&&r3==1) return ;
    if(r3==-1) cb+=num;
    else if(r2==-1) cw+=num;
    return ;
}
int main()
{
    int b,w,x,y;
    while(scanf("%d",&n)==1&&n)
    {
        memset(map,0,sizeof(map));
        memset(vis,false,sizeof(vis));
        cb=cw=0;
        scanf("%d%d",&b,&w);
        for(int i=0; i<b; i++)
            scanf("%d%d",&x,&y),map[x][y]=1;
        for(int i=0; i<w; i++)
            scanf("%d%d",&x,&y),map[x][y]=2;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(!map[i][j]&&!vis[i][j]) bfs(i,j);
        if(cb==cw) printf("Draw\n");
        else if(cb>cw) printf("Black wins by %d\n",cb-cw);
        else printf("White wins by %d\n",cw-cb);
    }
    return 0;
}

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

参考:http://blog.csdn.net/ehi11/article/details/8535872


,