码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ1010(BFS)

时间:2016-03-27 23:40:50      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:

//为什么bfs不行呢,想不通

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 10
int buf[maxn][maxn],vis[maxn][maxn]={false};
int n,m,t;
struct node
{
int x,y;
};
node st,ed;
int movx[4]={0,0,1,-1};
int movy[4]={1,-1,0,0};
int check(int x,int y)
{
if(x<0||x>=n)return 0;
else if(y<0||y>=m)return 0;
else if(buf[x][y]==-1)return 0;
else if (vis[x][y]==true)return 0;
return 1;
}
void bfs_traverse(node s)
{
queue < node >q;
q.push(s);
vis[s.x][s.y]=true;
while(!q.empty())
{
node tmp=q.front();q.pop();
vis[tmp.x][tmp.y]=true;
if(tmp.x==ed.x&&tmp.y==ed.y)return ;
for(int i=0;i<4;i++)
{
int x=tmp.x+movx[i],y=tmp.y+movy[i];
if(check(x,y))
{
node s;
s.x=x;s.y=y;
if(vis[x][y]==false)
{
buf[x][y]=buf[tmp.x][tmp.y]+1;
vis[x][y]=true;
}
q.push(s);
}
}
}
}
int main()
{
freopen("input.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
getchar();
memset(buf,0,sizeof(buf));
memset(vis,false,sizeof(vis));
if(!n&&!m&&!t)break;
int i,j;
char c;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%c",&c);
if(c==‘S‘)
{
st.x=i;
st.y=j;
}
else if(c==‘D‘)
{
ed.x=i;
ed.y=j;
}
else if(c==‘X‘)buf[i][j]=-1;
}
getchar();
}
bfs_traverse(st);


if(buf[ed.x][ed.y]==t)printf("YES\n");
else printf("NO\n");
}
return 0;
}

HDOJ1010(BFS)

标签:

原文地址:http://www.cnblogs.com/zeroArn/p/5327222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!