POJ1562 Oil Deposits(图论入门)

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
char map[111][111];
int n, m;
void dfs(int x, int y)
{
    map[x][y] = '*';
    if(x-1 >= 0 && map[x-1][y] == '@')
        dfs(x-1, y);
    if(x+1 < n && map[x+1][y] == '@')
        dfs(x+1, y);
    if(x-1 >= 0 && y-1 >= 0 && map[x-1][y-1] == '@')
        dfs(x-1, y-1);
    if(y-1 >= 0&& map[x][y-1] == '@')
        dfs(x, y-1);
    if(x-1 >= 0 && y+1 < m && map[x-1][y+1] == '@')
        dfs(x-1, y+1);
    if(y+1 < m && map[x][y+1] == '@')
        dfs(x, y+1);
    if(x+1 < n && y+1 < m && map[x+1][y+1] == '@')
        dfs(x+1, y+1);
    if(x+1 < n && y-1 >= 0 && map[x+1][y-1] == '@')
        dfs(x+1, y-1);

}
int main(void)
{
//    freopen("1.txt", "r", stdin);

    while(cin>>n>>m && m+n)
    {
        memset(map, 0, sizeof(map));
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                cin>>map[i][j];
        int sum = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                if(map[i][j] == '@')
                {
                    sum++;
                    dfs(i, j);
                }
            }

        cout<<sum<<endl;
    }
    return 0;
}

你可能感兴趣的:(POJ1562 Oil Deposits(图论入门))