水题,代码如下
#include <iostream>
#include <cstdio>
using namespace std;
char a[22][22];
int dr[4][2] = {{-1, 0}, {1, 0}, {0, -1},{0, 1}};
int x, y;
int w, h;
int num = 0;
void dfs(int x, int y)
{
int m, n;
for (int i = 0; i < 4; i++)
{
m = x + dr[i][0];
n = y + dr[i][1];
if (m >= 0 && m < h && n >= 0 && n < w)
if (a[m][n] == '.')
{
num++;
a[m][n] = '#';
dfs(m, n);
}
}
}
int main()
{
while (scanf("%d %d", &w, &h), w != 0 && h != 0)
{
getchar();
num = 0;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
scanf("%c", &a[i][j]);
if (a[i][j] == '@')
{
x = i;
y = j;
}
}
getchar();
}
num++;
dfs(x, y);
cout << num << endl;
}
return 0;
}