UVa 572 - Oil Deposits

传送门UVa 572 - Oil Deposits

应该是最简单的图的题目吧, 够水了, 和书上图的例题差不多.

不多说了.


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

char oil[110][110];

void DFS(int x, int y);

int main()
{
    //freopen("input.txt", "r", stdin);
    int row, col;
    int i, j, cnt;
    while (scanf("%d%d", &row, &col))
    {
        getchar();
        memset(oil, 0, sizeof(oil));
        cnt = 0;
        if (row + col == 0)
            break;
        for (i = 1; i <= row; i++)
            scanf("%s", oil[i] + 1);
        for (i = 1; i <= row; i++)
            for (j = 1; j <= col; j++)
            {
                if (oil[i][j] == '@')
                {
                    cnt++;
                    DFS(i, j);
                }
            }
        printf("%d\n", cnt);
    }
    return 0;
}

void DFS(int x, int y)
{
    if (oil[x][y] != '@')
        return;
    else
    {
        oil[x][y] = '*';    //表示已经访问过.
        for (int i = -1; i <= 1; i++)
            for (int j = -1; j <= 1; j++)
                DFS(x + i, y + j);
    }
}


你可能感兴趣的:(ACM,uva)