您的位置:首页 > 其它

2013寒假练习 1038 Red and Black

2013-02-16 21:23 246 查看
地址: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: