WUST 2050 三维迷宫(bfs)
来源:互联网 发布:python 列表复制 编辑:程序博客网 时间:2024/05/21 09:06
2050: 三维迷宫
Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lldSubmitted: 50 Accepted: 12
[Submit][Status][Web Board]
Description
你现在处于一栋楼里的'S'的起始位置,'#'是墙壁,'.'是路,'E'是重点,'U'的楼梯代表你可以上去一层,'D'的楼梯代表你可以下去一层(保证上去跟下去的地方不会是墙壁)。请你告诉我至少要多少步才能到达终点,当然有可能去不到重点,这时请输出"Trapped!"
Input
输入一个L、N、M,L代表层数,N,代表每层的长,M代表每层的宽。接下来有L个N*M的图形,代表从第1层到第L层的地图。保证 L,N,M均不大于50
Output
输入最小要多少步才能到终点,如果出不去,请输出"Trapped!"。
Sample Input
3 5 5S...U#####..###....U#####......####..###D###U#####......####..######E.#####1 3 3S.###E###
Sample Output
20Trapped!
Author
ControlBear
题解:
不是很难的一道bfs题,处理起来有点麻烦而已
代码:
#include<iostream>#include<stdio.h>#include<map>#include<algorithm>#include<math.h>#include<queue>#include<stack>#include<string>#include<cstring>using namespace std;char p[55][55][55];//存图int vis[55][55][55];//遍历数组int dirx[4]={0,0,-1,1};int diry[4]={1,-1,0,0};int l,n,m;int sc,sx,sy,ec,ex,ey;//存起始点坐标struct node{ int c,x,y; int step;};queue<node>q;int main(){ int i,j,k,tag,s; node now,next; while(scanf("%d%d%d",&l,&n,&m)!=EOF) { while(!q.empty())q.pop(); for(i=0;i<l;i++) { for(j=0;j<n;j++) memset(vis[i][j],0,sizeof(vis[0][0][0])*(m+1));//初始化 } for(i=0;i<l;i++) { for(j=0;j<n;j++) { scanf("%s",p[i][j]); for(k=0;k<m;k++) { if(p[i][j][k]=='S') { sx=j; sy=k; sc=i; } else if(p[i][j][k]=='E') { ex=j; ey=k; ec=i; } } } } now.c=sc,now.step=0,now.x=sx,now.y=sy; tag=0; q.push(now); vis[sc][sx][sy]=1; while(!q.empty())//日常宽搜 { now=q.front(); q.pop(); if(now.c==ec&&now.x==ex&&now.y==ey) { s=now.step; tag=1; break; } if(p[now.c][now.x][now.y]=='D') { if(now.c-1>=0&&!vis[now.c-1][now.x][now.y]) { vis[now.c-1][now.x][now.y]=1; next=now; next.c--; next.step++; q.push(next); } } else if(p[now.c][now.x][now.y]=='U') { if(now.c+1<l&&!vis[now.c+1][now.x][now.y]) { vis[now.c+1][now.x][now.y]=1; next=now; next.c++; next.step++; q.push(next); } } for(i=0;i<4;i++) { int xx=dirx[i]+now.x; int yy=diry[i]+now.y; if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[now.c][xx][yy]&&p[now.c][xx][yy]!='#') { vis[now.c][xx][yy]=1; next.x=xx; next.y=yy; next.c=now.c; next.step=now.step+1; q.push(next); } } } if(tag) printf("%d\n",s); else printf("Trapped!\n"); } return 0;}
阅读全文
0 0
- WUST 2050 三维迷宫(bfs)
- POJ 2251 (三维迷宫问题) DFS /BFS
- POJ 2251 Dungeon Master (三维迷宫 BFS)
- POJ2251——BFS三维迷宫
- POJ 2251 (BFS 简单三维迷宫)
- POJ 2251 Dungeon Master【三维迷宫裸BFS练习】
- POJ 2049-Finding Nemo(三维bfs解决类迷宫问题)
- HDU 2102 A计划 (三维的迷宫BFS)
- POJ NO.2251 Dungeon Master(BFS,三维迷宫)
- POJ-2251-Dungeon Master【BFS】【三维迷宫问题】
- POJ2251---Dungeon Master---BFS最短路之三维迷宫
- POJ 2251-Dungeon Master(BFS-三维迷宫)
- 三维迷宫
- 三维bfs
- BFS~~~迷宫
- BFS(迷宫)
- 迷宫-BFS
- BFS------迷宫
- 弦图(草稿)
- Xmind:夏日也不能忘了健身哦!
- 20170724_父进程和子进程到底是如何执行的?
- tree
- 微信jssdk获取当前位置,以及打开微信地图
- WUST 2050 三维迷宫(bfs)
- 我的JavaScript学习笔记
- hdu1527
- 一个m*n的矩阵,从左到右从上到下都是递增的,给一个数x,判断x是否在矩阵中。要求效率尽可能的高。
- Android 使用sharedpreferences、ACache缓存用户名和密码
- sublime text3使用
- java8 forEach案例
- Perfect Squares 费马小定理求乘法逆元k=b^(p-2)
- Linux下Redis集群安装部署及使用详解