HDU 1728 逃离迷宫 BFS

来源:互联网 发布:外国人怎么看中国 知乎 编辑:程序博客网 时间:2024/05/22 01:44

看题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1728

这题的x1,x2对应列,y1, y2对应行 。。很邪恶

还要注意下标应该从1开始。。。。一时傻了调了半天。

还有前后坐标一样的时候。。。。贡献一次WA

同样的还是BFS的应用。

只不过是用一个step来标记目前转过的弯,每条路一路走到底。



#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int MAXN=100+10;int n,m,k;char maze[MAXN][MAXN];bool vis[MAXN][MAXN];const int dx[]={0,1,0,-1};const int dy[]={-1,0,1,0};struct site{int x,y;int step;site(int i=-1,int j=-1,int s=-1){x=i;y=j;step=s;}}start,en;bool bfs(){if(start.x==en.x && start.y==en.y)return true;memset(vis,0,sizeof(vis));vis[start.x][start.y]=true;start.step=-1;queue<site> q;q.push(start);while(!q.empty()){site temp=q.front();q.pop();for(int i=0;i<4;i++){int nx=temp.x+dx[i];int ny=temp.y+dy[i];while(nx >=1 && ny >=1 && nx<=m && ny<=n && maze[nx][ny]=='.'){if(vis[nx][ny]==false){q.push(site(nx,ny,temp.step+1));vis[nx][ny]=true;if(nx==en.x && ny==en.y && temp.step+1<=k)return true;}nx=nx+dx[i];ny=ny+dy[i];}}}return false;}int main(){int t;scanf("%d",&t);while(t--){scanf("%d%d",&m,&n);for(int i=1;i<=m;i++)scanf("%s",maze[i]+1);scanf("%d%d%d%d%d",&k,&start.y,&start.x,&en.y,&en.x);if(bfs())printf("yes\n");else printf("no\n");}}



原创粉丝点击