1012 - Guilty Prince

/*
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.

*/

// Algorithm: BFS (2D)
// Accepted

#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 = 50;


struct printf
{
    void ek(int n){cout << n << "\n";}
    void dui(int x , int y) { cout << x << " " << y << "\n"; }
    void tin(int x , int y , int z) { cout << x << " " << y << " " << z << "\n"; }
}tp;

struct sieve
{
    int prm[high], plen=0;
    bitset<high>bs;
    void get_prime()
    {
        LL i , j;
        bs.set();
        bs[0]=bs[1]=0;
        for(i=2; i<=high; i++)
        {
            if(bs[i])
            {
                prm[plen++] = i;

                for(j=i*i; j<=high; j+=i) bs[j] = 0;
            }
        }
    }
    void pprm(){ for(int i=0; i<100; i++) cout << prm[i] << "; "; }
}prime;

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

void BFS(int x , int y, int row, int col)
{
    queue<int>Q;
    Q.push(x);
    Q.push(y);

    visited[x][y] = true;
    cnt = 1;

    int ux , uy , vx , vy;

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

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

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

            if((vx>=0 && vx<row) && (vy>=0 && vy<col) && (adj[vx][vy] != '#'))
            {
                if(!visited[vx][vy])
                {
                    cnt++;
                    visited[vx][vy] = true;
                    Q.push(vx);
                    Q.push(vy);
                }
            }
        }
    }
}

int main()
{
    //fast;
    int i , j , test , tc=0 , W , H , row , col , er , ec;
    char ch;

    sf("%d", &test);

    while(test--)
    {
        mset(adj , '#');
        mset(visited , false);
        cnt=0;

        sf("%d %d", &W , &H);

        row = H;
        col = W;

        for(i=0; i<row; i++)
        {
            for(j=0; j<col; j++)
            {
                sf(" %c", &ch);
                //cin >> ch;

                if(ch == '@')
                {
                    er = i;
                    ec = j;
                }

                adj[i][j] = ch;
            }
        }

        BFS(er , ec , row , col);

        pf("Case %d: %d\n", ++tc , cnt);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number