UVa 572 Oil Deposit (DFS)

so easy!

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 110;
int n, m, ans;
char g[N][N];
int a[] = { 0, 0, 1, -1, 1, -1, -1, 1 };
int b[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
void dfs( int x, int y ) {
    g[x][y] = '*';
    for ( int i = 0; i < 8; ++i ) {
        int xx = x + a[i], yy = y + b[i];
        if ( g[xx][yy] == '@' ) dfs ( xx, yy );
    }
}
int main()
{
    while ( scanf("%d%d", &n, &m) != EOF && m ) {
        getchar(); ans = 0;
        memset(g, '\0', sizeof(g));
        for ( int i = 0; i < n; ++i ) scanf("%s", g[i]);
        for ( int i = 0; i < n; ++i )
            for ( int j = 0; j < m; ++j ) 
                if ( g[i][j] == '@' ) {
                    ans++;
                    dfs( i, j );
                }
        printf("%d\n", ans);
    }
}


你可能感兴趣的:(UVa 572 Oil Deposit (DFS))