简单搜索题。

思路:先将地图预处理。如果遇到没有摆放棋子的地方则进行搜索,如果这个区域内黑色棋子的数目或者白色棋子的数目为0的话,则可以将这片区域的面积加到白色棋子或者黑色棋子控制区域的总数。最后判断黑色棋子控制的范围与白色棋子控制的范围谁更大。

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

const int SIZE = 21;
const int move[4][2] = {{10}, {-10}, {0, -1}, {01}};
char maze[SIZE][SIZE];
int vis[SIZE][SIZE];
int n, tw, tb, ts, B, W;


int check(int r, int c)
{
    if(r >= 0 && c >= 0 && r <= n && c <= n && vis[r][c] == 0return 1;
        return 0;
}


void dfs(int x, int y)
{
    int i;
    vis[x][y] = 1
    ts++;
    for(i = 0; i < 4; i++)
    {
        int xx = x+move[i][0]; 
        int yy = y+move[i][1];
        if(check(xx, yy))
        {
            if(maze[xx][yy] == ' ')
                dfs(xx, yy);
            if(maze[xx][yy] == 'B')
                tb++;
            if(maze[xx][yy] == 'W')
                tw++;
        }
    }
} //dfs

void init()        //init
{
    int i, j;
    int r, c;
    for(i = 1 ; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            maze[i][j] = ' ';
        }
    }
    for(i = 0; i < B; i++)
    {
        scanf("%d%d", &r, &c);
        maze[r][c] = 'B';
    }
    for(i = 0; i < W; i++)
    {
        scanf("%d%d", &r, &c);
        maze[r][c] = 'W';
    }
        return ;
}

int main()
{
    int i, j;
    int n1, n2;
    while(~scanf("%d", &n), n)
    {
        n1 = n2 = 0;
        scanf("%d%d", &B, &W);
        init();
        memset(vis, 0sizeof(vis));
        
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= n; j++)
            {
                if(maze[i][j] == ' ' && !vis[i][j])
                {
                    tw = tb = ts = 0;
                    dfs(i, j);
                    if(!tw)    n1 += ts;          //tw == 0
                    if(!tb)    n2 += ts;             //tb == 0
                }
            }
        }
        if(n1 == n2) printf("Draw\n");
        else if(n1 > n2) 
        {
            printf("Black wins by %d\n", n1-n2);
        }
        else printf("White wins by %d\n", n2-n1);
    }
    return 0;

} 

posted on 2012-08-23 11:46  有间博客  阅读(202)  评论(0编辑  收藏  举报