裸bfs。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
char s[25];
int map[25][25],n,m,cnt,bx,by;
int dx[]={0,1,0,-1,0},dy[]={0,0,1,0,-1};
bool vis[25][25];
struct code
{
int x,y;
};
queue <code> q;
bool judge(int x,int y)
{
if ((x>=1) && (x<=n) && (y>=1) && (y<=m) && (vis[x][y]==false) && (map[x][y]==1))
return true;
return false;
}
void bfs(int bx,int by)
{
code begin;
begin.x=bx;begin.y=by;
while (!q.empty())
q.pop();
vis[bx][by]=true;cnt++;
q.push(begin);
while (!q.empty())
{
code head=q.front();
q.pop();
for (int i=1;i<=4;i++)
{
int tx=head.x+dx[i],ty=head.y+dy[i];
if (judge(tx,ty)==true)
{
code now;
now.x=tx;now.y=ty;
vis[tx][ty]=true;
cnt++;
q.push(now);
}
}
}
}
int main()
{
while (scanf("%d%d",&n,&m)==2)
{
if ((n==0) && (m==0)) break;
swap(n,m);
memset(vis,false,sizeof(vis));
cnt=0;
for (int i=1;i<=n;i++)
{
scanf("%s",s);
for (int j=0;j<m;j++)
{
if (s[j]=='.') map[i][j+1]=1;
else if (s[j]=='#') map[i][j+1]=0;
else
{
bx=i;
by=j+1;
}
}
}
bfs(bx,by);
printf("%d\n",cnt);
}
return 0;
}