地址:http://acm.bit.edu.cn/mod/programming/view.php?id=689
水题,最基本的bfs。
#include<iostream>
#include<queue>
using namespace std;
typedef struct NODE
{
int x,y;
}node;
node temp,now;
char map[25][25];
bool flag[25][25];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int main()
{
int m,n,i,j;
while(scanf("%d%d",&n,&m),m||n)
{
int ans=0;
queue<node>q;
memset(flag,0,sizeof(flag));
for(i=0;i<m;i++)
{
getchar();
for(j=0;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='@') temp.x=i,temp.y=j,q.push(temp),ans++,flag[i][j]=1;
}
}
while(!q.empty())
{
temp=q.front();
for(i=0;i<4;i++)
{
now.x=temp.x+dir[i][0],now.y=temp.y+dir[i][1];
if(now.x>=0&&now.x<m&&now.y>=0&&now.y<n&&!flag[now.x][now.y]&&map[now.x][now.y]=='.')
{
q.push(now);
flag[now.x][now.y]=1,ans++;
}
}
q.pop();
}
printf("%d\n",ans);
}
return 0;
}