HDU 4158 GO

围棋,虽然不怎么懂围棋规则,这题题目说明:只有同色棋子围着的区域的空点属于该色旗子,哪方围的空点多哪方就胜利,问哪方胜利。
思路:BFS空位,搜到空位加入队列,然后统计空位的个数+1,搜到棋子标记该种颜色棋子出现,但不加入队列,搜完一个区域后,看是否两种棋子都出现,如果是,该区域就不属于任何一方,如果不是,把该区域的空位数+到相应方。搜索知道所有的空位都遍历过为止。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int map1[21][21],w,b,n,totalb,totalw;
struct point
{
    int x,y;
};
void BFS(int x,int y)
{
    int flagw=0,flagb=0,ans=0;
    queue<point> q;
    while(!q.empty()) q.pop();
    point a;
    a.x = x;
    a.y = y;
    map1[x][y]=3;
    q.push(a);
    while(!q.empty())
    {
        point b;
        b = q.front();
        ans++;
        q.pop();
        for(int i = 0 ; i < 4 ; i++)
        {
            point c;
            c.x = b.x+dir[i][0];
            c.y = b.y+dir[i][1];
            if(c.x>=1&&c.x<=n&&c.y>=1&&c.y<=n)
            {
                if(map1[c.x][c.y]==0)
                {
                    q.push(c);
                    map1[c.x][c.y]=3;
                }
                else if(map1[c.x][c.y]==1)
                    flagb = 1;
                else if(map1[c.x][c.y]==2)
                    flagw = 1;

            }
        }
    }
    if(flagb+flagw!=2)
    {
        if(flagb) totalb+=ans;
        else if(flagw) totalw+=ans;
    }

}
int main()
{

    while(~scanf("%d",&n))
    {
        totalb = totalw =0;
        if(n==0) return 0;
        scanf("%d%d",&b,&w);
        memset(map1,0,sizeof(map1));
        for(int i = 0 ; i < b; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            map1[x][y]=1;
        }
        for(int i = 0 ; i < w; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            map1[x][y]=2;
        }
        for(int i = 1; i <=n ; i++)
        {
            for(int j = 1 ; j <= n ; j++)
                if(map1[i][j]==0)
                BFS(i,j);
        }
        if(totalb==totalw) printf("Draw\n");
        else if(totalb>totalw) printf("Black wins by %d\n",totalb-totalw);
        else printf("White wins by %d\n",totalw-totalb);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值