hdu 4158 GO

http://acm.hdu.edu.cn/showproblem.php?pid=4158

题意:给一个n*n的棋盘,再给一些黑白棋的位置,求哪个着色完全围着的空白多。(难道这就是围棋?)

思路:模拟~~~

View Code
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
const int maxn = 30;
int as[maxn][maxn];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,-1,1};
int c1,c2;
int n,w,b;
void bfs(int x,int y)
{
    int tx,ty,i;
    int f1=0,f2=0,k=1;
    queue<int>qx; queue<int>qy;
    qx.push(x); qy.push(y);
    as[x][y]=2;
    while(!qx.empty()){
        tx = qx.front(); qx.pop();
        ty = qy.front(); qy.pop();
        for(i = 0; i < 4; ++ i){
            x = tx + dx[i];
            y = ty + dy[i];
            if(x<=0||y<=0||x>n||y>n)continue;
            if(as[x][y]<0)f1=1;
            if(as[x][y]==1)f2=1;
            if(as[x][y])continue;
            as[x][y]=2;
            k++;
            qx.push(x);
            qy.push(y);
        }
    }
    if(f1&&f2)return;
    if(f1)c1+=k;
    if(f2)c2+=k;
}
int main()
{
    while(scanf("%d",&n)==1){
        if(!n)break;
        scanf("%d %d",&b,&w);
        memset(as,0,sizeof(as));
        for(int i = 0; i < b; ++ i){
            int x,y;
            scanf("%d %d",&x,&y);
            as[x][y] = -1;
        }
        for(int i = 0; i < w; ++ i){
            int x,y;
            scanf("%d %d",&x,&y);
            as[x][y] = 1;
        }
        c1 = c2 = 0;
        for(int i = 1; i <= n; ++ i)
        for(int j = 1; j <= n; ++ j)
        if(!as[i][j])bfs(i,j);
        if(c1==c2)puts("Draw");
        else if(c1<c2)printf("White wins by %d\n",c2-c1);
        else printf("Black wins by %d\n",c1-c2);
    }
    return 0;
}

posted on 2012-10-11 10:41  aigoruan  阅读(135)  评论(0)    收藏  举报

导航