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;
}
}
#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;
}
}
相关文章推荐
- poj 1979 &&hdu 1312Red and Black (bfs)
- (step4.2.2)hdu 1312(Red and Black——BFS)
- HDU 1312 Red and Black(BFS)
- HDU1312 Red and Black (DFS || BFS)
- HDU1312 Red and Black(BFS)(step与sum)
- HDU - 1312 : Red and Black(dfs、bfs)
- hdu 1312 Red and Black(bfs实现)
- HDU 1312 Red and Black(简单BFS)
- hdu 1312 Red and Black(BFS水题)
- HDU 1312 Red and Black(经典搜索,DFS&BFS三种方式)
- HDU 1312 Red and Black(BFS,DFS)
- HDU 1312 Red and Black(基础bfs或者dfs)
- hdu 1312 Red and Black (dfs+bfs)
- HPU DFS + BFS 专项练习A - Red and Black HDU - 1312
- HDU 1312 Red and Black-dfs&bfs-(分块)
- HDU 1312Red and Black(简单搜索 bfs或dfs)
- HDU 1312 Red and Black <BFS>
- HDU 1312 Red and Black(并查集或者BFS)
- HDU 1312:Red and Black(BFS)
- Hdu 1312 Red and black(BFS)