Uva 469 Solution

#include<bits/stdc++.h>
#define pii pair<int, int>
#define N 103
using namespace std;

int x[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int y[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int row, column, v[N][N], cnt = 0;
char a[N][N];

bool check(int r, int c)
{
    return (r>=0) && (r<row) && (c>=0) && (c<column);
}

int bfsvisit(int x1,int y1)
{
    v[x1][y1] = 1;
    cnt = 1;
    queue<pii>q;
    q.push(pii(x1, y1));

    while(!q.empty())
    {
        pii r = q.front();
        q.pop();

        for(int i=0; i<8; i++)
        {
            int tx = r.first + x[i];
            int ty = r.second + y[i];

            if(check(tx, ty) && a[tx][ty] == 'W' && v[tx][ty] == 0)
            {
                v[tx][ty] = 1;
                cnt++;
                q.push(pii(tx, ty));
            }
        }
    }

    return cnt;
}

void clear1()
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
a[i][j] = '\0';
v[i][j] = 0;
}
}
}

void clear2()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
v[i][j] = 0;
}
}
}

int main()
{
    string s;
    int t, k = 0;

    cin >> t;
    getchar();
    getchar();
    while(t--)
{
int c[2] = {0}, f = 1;
row = column = 0;
if(k > 0)
cout << "\n";
k = 1;

clear1();

while(getline(cin, s))
{
if(s[0] != 'L' && s[0] != 'W')
{
if(s == "")
break;

int i = 0, w;
stringstream st(s);
while(st >> w)
c[i++] = w;

int r = bfsvisit(c[0]-1, c[1]-1);
cout << r << "\n";

clear2();
}
else
{
if(f == 1)
{
column = s.size();
f = 0;
}
for(int i=0;i<s.size();i++)
a[row][i] = s[i];
row++;
}
}
}

    return 0;
}

Comments