题目链接:http://poj.org/problem?id=1979
题意:迷宫问题的简单化,暴力一遍,记录能经过的红色区域即可。
ac代码
#include<cstdio>
#include<string.h>
#define M 25
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
int count;
int m, n;
char maze[M][M];
void dfs(int x, int y)
{
count++;
maze[x][y] = '#';
for (int i = 0; i < 4; i++)
if (maze[x + dx[i]][y + dy[i]] == '.' && x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < m)
{
maze[x + dx[i]][y + dy[i]] = '#';
dfs(x + dx[i], y + dy[i]);
}
}
int main()
{
int t, k;
while (scanf("%d %d", &m, &n) != -1)
{
count = 0;
if (m == 0 && n == 0)break;
for (int i = 0; i < n; i++)
{
getchar();
for (int j = 0; j < m; j++)
{
scanf("%c", &maze[i][j]);
if(maze[i][j] == '@')
{
t = i;
k = j;
}
}
}
dfs(t, k);
printf("%d\n", count);
}
return 0;
}