Tuesday, April 19, 2016

UVa 572 - Oil Deposits

#include <bits/stdc++.h>
#define LL  long long
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define pri(a) cout << a << endl;
using namespace std;

char gr[105][105];
int r,c;

void DFS(int i, int j)
{
    if(i < 0 or j < 0 or i == r or j == c or gr[i][j] != '@') return;

    gr[i][j] = '*';

    DFS(i-1,j);
    DFS(i-1,j+1);
    DFS(i-1,j-1);
    DFS(i,j+1);
    DFS(i,j-1);
    DFS(i+1,j);
    DFS(i+1,j+1);
    DFS(i+1,j-1);

    return;
}

int main()
{
    while(cin>>r>>c and r+c)
    {
        REP(i,r) cin>>gr[i];

        int cnt = 0;

        REP(i,r)
        {
            REP(j,c)
            {
                if(gr[i][j] == '@')
                {
                    cnt++;
                    DFS(i,j);
                }
            }
        }
        cout << cnt << "\n";
    }
    return 0;
}

No comments:

Post a Comment