HUD 1312 Red and Black(用深搜写的)

#include <iostream>
using namespace std;

char g[25][25];
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int sum;
int n, m;

void Dfs(int x, int y)
{
    int a, b;
    for (int i = 0; i < 4; i++)
    {
        a = x + dir[i][0];
        b = y + dir[i][1];
        if (a >= 1 && a <= n && b >= 1 && b <= m && g[a][b] == '.')
        {
            sum++;
            g[a][b] = '#';
            Dfs(a, b);
        }
    }
}
int main()
{

    int star_x, star_y;
    while (scanf("%d%d", &m, &n), n + m)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                scanf(" %c", &g[i][j]);
                if (g[i][j] == '@')
                {
                    g[i][j] = '#';
                    star_x = i;
                    star_y = j;
                }
            }
        }
        sum = 1;
        Dfs(star_x, star_y);
        printf("%d\n", sum);
    }

    return 0;
}

 

posted on 2012-05-26 10:57  [S*I]SImMon_WCG______*  阅读(274)  评论(0编辑  收藏  举报

导航