Sunday, August 17, 2014

uva : 260 - Il Gioco dell'X


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int main()
{
    //freopen("input.txt","r",stdin);
    int row,test=1;
    while(scanf("%d\n",&row)==1&&row!=0)
    {
        int number[row+1][row+1],i,j;
        queue<int>r,c;
        for(i=1;i<=row;i++)
        {
            for(j=1;j<=row;j++)
            {
                char ch;
                scanf("%c",&ch);
                if(i==1)
                {
                    if(ch=='b'){
                    r.push(i);
                    c.push(j);}
                    number[i][j]=0;
                }
                else if(ch=='b')
                {
                    number[i][j]=1;
                }
                else number[i][j]=0;
            }
            getchar();
        }
        int flag=0;
        while(!r.empty())
        {
            int x=r.front();
            int y=c.front();
            if(x==row)
            {
                printf("%d B\n",test++);
                flag=1;
                break;
            }
            r.pop();
            c.pop();
            if(x-1>0)
            {
                if(number[x-1][y]==1)
                {
                    r.push(x-1);
                    c.push(y);
                    number[x-1][y]=0;
                }
            }
            if(x+1<=row)
            {
                if(number[x+1][y]==1)
                {
                    r.push(x+1);
                    c.push(y);
                    number[x+1][y]=0;
                }
            }
            if(y-1>0)
            {
                if(number[x][y-1]==1)
                {
                    r.push(x);
                    c.push(y-1);
                    number[x][y-1]=0;
                }
            }
            if(y+1<=row)
            {
                if(number[x][y+1]==1)
                {
                    r.push(x);
                    c.push(y+1);
                    number[x][y+1]=0;
                }
            }
            if(x-1>0&&y-1>0&&number[x-1][y-1]==1)
            {
                r.push(x-1);
                c.push(y-1);
                number[x-1][y-1]=0;
            }
            if(x+1<=row&&y+1<=row&&number[x+1][y+1]==1)
            {
                r.push(x+1);
                c.push(y+1);
                number[x+1][y+1]=0;
            }
        }
        //printf("%d\n",test);
        if(flag==0)printf("%d W\n",test++);
    }
    return 0;
}

No comments:

Post a Comment