您的位置:首页 > 其它

hdu 1312 red and black BFS

2013-07-24 11:12 507 查看
熟悉一下BFS的写法。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cctype>
using namespace std;
int f[4][2]={0,1,1,0,0,-1,-1,0};
int n,m,tmp;
char a[21][21];
struct node{
int x,y;
};
node e,v,s;
queue<node>que;
int bfs()
{
que.push(s);
while(!que.empty())
{
e=que.front();
que.pop();
for(int i=0;i<4;i++)
{
v=e;
v.x=e.x+f[i][0];
v.y=e.y+f[i][1];
if(a[v.x][v.y] == '#')
continue;
if(a[v.x][v.y] == '.'&&v.x<=m&&v.y<=n&&v.x>=1&&v.y>=1)
{
que.push(v);
a[v.x][v.y]='#';
tmp++;
}
}
}
return tmp;
}
int main()
{
while(cin>>n>>m)
{
getchar();
if(n == 0&& m == 0)
break;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j] == '@')
{
s.x=i;
s.y=j;
}
}
tmp=1;
cout<<bfs()<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: