LightOJ 1012 (简单dfs)

题解:

简单的dfs计数…

代码

#include<iostream>
using namespace std;
char map[20][20];
int W, H;
void pos(char (*map)[20], int &sum, int dx, int dy)
{
        if (map[dx][dy] == '.')
        {
            map[dx][dy] = '*';
            sum++;
            if (dx >0)
                pos(map, sum, dx - 1, dy);
            if (dy>0)
                pos(map, sum, dx, dy - 1);
            if (dx < H - 1)
                pos(map, sum, dx + 1, dy);
            if (dy<W - 1)
                pos(map, sum, dx, dy + 1);
        }

}
int main()
{
    int n, i, j, k,sum = 0,dx,dy;
    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> W >> H;
        for (j = 0; j < H;j++)
        for (k = 0; k < W; k++)
        {
            cin >> map[j][k];
            if (map[j][k] == '@')
            {
                dx = j;
                dy = k;
                map[j][k] = '.';
            }
        }
        pos(map,sum,dx,dy);
        cout << "Case "<<  i+1 << ": "<< sum << endl;
        sum = 0;
    }
}

你可能感兴趣的:(LightOJ 1012 (简单dfs))