题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312
#include <cstdio>
#include <iostream>
using namespace std;
char a[22][22];
int total = 0;
void dfs(int x, int y);
int main()
{
int w, h;
while(1)
{
int x=0,y=0;
scanf("%d %d",&w,&h);
if(w==0 && h==0)
break;
for(int i=0; i<h; i++)
{
scanf("%s",&a[i]);
getchar();
}
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
if(a[i][j]=='@')
{
x=i;
y=j;
}
}
}
total = 1;
dfs(x,y);
printf("%d\n",total);
}
return 0;
}
void dfs(int x, int y)
{
a[x][y] = '#';
if(a[x-1][y]=='.')
{
total++;
dfs(x-1,y);
}
if(a[x+1][y]=='.')
{
total++;
dfs(x+1,y);
}
if(a[x][y-1]=='.')
{
total++;
dfs(x,y-1);
}
if(a[x][y+1]=='.')
{
total++;
dfs(x,y+1);
}
return;
}