UVa 260 - Il Gioco dell'X

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

// Accepted
// BFS [Directional Array]

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back

typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;

const int mod = 1000007;
const int high = 250;

char adj[high][high];
int dir_x[]={-1, -1, 0 , 0, 1, 1}, dir_y[]={-1, 0, -1, 1, 0, 1}, visited[high][high], N;

void BFS(int x , int y)
{
    visited[x][y] = 1;
    queue<int>Q;
    Q.push(x);
    Q.push(y);
    int ux, uy, vx,vy;

    while(!Q.empty())
    {
        ux = Q.front();
        Q.pop();

        uy = Q.front();
        Q.pop();

        for(int i=0; i<6; i++)
        {
            vx = ux + dir_x[i];
            vy = uy + dir_y[i];

            if((vx>=0 && vx<N) && (vy>=0 && vy<N) && (adj[vx][vy] == 'w'))
            {
                if(!visited[vx][vy])
                {
                    visited[vx][vy] = 1;
                    Q.push(vx);
                    Q.push(vy);
                }
            }
        }
    }
}

int main()
{
    //fast;
    int i , j , tc=0;
    while(~sf("%d", &N) && N)
    {
        mset(visited , 0);

        for(i=0; i<N; i++)
        {
            for(j=0; j<N; j++)
            {
                sf(" %c", &adj[i][j]);
            }
        }

        for(i=0; i<N; i++) // column wise, so we have to find White
        {
            if(adj[i][0] == 'w' && visited[i][0] == 0)
            {
                BFS(i , 0);
            }
        }

        int flag = 0;

        for(i=0; i<N; i++)
        {
            if(visited[i][N-1] == 1)
            {
                flag = 1;
                break;
            }
        }

        pf("%d ", ++tc);

        if(flag) pf("W\n");
        else pf("B\n");
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number