UESTC 方老师与迷宫
2014-11-01 23:55
267 查看
方老师被困在了一个3维的迷宫中,他很想逃离这个迷宫回去玩炉石传说,你能帮助他么?
3维bfs 六个方向搜就行了
Input
多组测试数据,对于每组测试数据,有三个整数 L,R,C(0<l,r,c≤30)。L代表迷宫的高度,R和C分别代表每一层的行和列。接下来是L个R×C的矩阵,矩阵包含4种字符(S,
E,
.,
#),
S代表方老师的初始位置,
E代表出口,
#代表障碍。
.代表能通过的地方。每一层之后有一个空行。当L=R=C=0时,数据中断。
Output
如果可以逃离迷宫,按下列格式输出最短时间:Escaped in x minute(s).(x表示逃离迷宫的最短时间)否则,输出:
Trapped!
Sample input and output
Sample Input | Sample Output |
---|---|
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0 | Escaped in 11 minute(s). Trapped! |
#include <iostream>#include <stdio.h>#include <queue>#include <string.h>using namespace std;char map[35][35][35];bool vis[35][35][35];int L,R,C;int e_x,e_y,e_z;int x[7] = {1,-1,0,0,0,0};int y[7] = {0,0,1,-1,0,0};int z[7] = {0,0,0,0,1,-1};struct node{int x,y,z;int step;bool operator<(const node &t)const{return t.step<step;}}s;bool judge(int x,int y,int z){if(x>=0 && x<R && y>=0 && y<C && z>=0 && z<L && !vis[x][y][z] && map[z][x][y]!= '#'){return true;}return false;}int bfs(){priority_queue<node>q;q.push(s);memset(vis, false, sizeof(vis));node t,h;vis[s.x][s.y][s.z]=true;while (!q.empty()){t=q.top();if(t.x==e_x && t.y==e_y && t.z==e_z){return t.step;}q.pop();for (int i=0; i<6; i++){h.x=t.x+x[i];h.y=t.y+y[i];h.z=t.z+z[i];h.step=t.step+1;if(judge(h.x, h.y, h.z)){q.push(h);vis[h.x][h.y][h.z]=true;}}}return -1;}int main(){while (scanf("%d%d%d",&L,&R,&C)!=EOF && (L|| R || C)){for (int i=0; i<L; i++){for (int j=0; j<R; j++){scanf("%s",map[i][j]);for (int k=0; k<C; k++){if(map[i][j][k]=='S'){s.x=j;s.y=k;s.z=i;s.step=0;}else if(map[i][j][k]=='E'){e_x=j;e_y=k;e_z=i;}}}getchar();}int ans=bfs();if(ans==-1){printf("Trapped!\n");}else{printf("Escaped in %d minute(s).\n",ans);}}return 0;}
相关文章推荐
- uestc 方老师与迷宫(三维迷宫)
- 2014 UESTC Training for Search Algorithm Problem F 方老师与迷宫
- UESTC 886 方老师金币堆 --合并石子DP
- UESTC 917 方老师的分身IV --求欧拉路径
- uestc 方老师开橙卡
- uestc 方老师和缘分
- UESTC_王之迷宫 2015 UESTC Training for Search Algorithm & String<Problem A>
- UESTC_方老师的分身 II CDOJ 915
- 2014 UESTC Training for Graph Theory Problem H 方老师的分身 II
- CDOJ (UESTC) 851 方老师与素数(bfs)
- CDOJ 方老师与迷宫
- 2014 UESTC Training for Graph Theory Problem I 方老师的分身 III
- UESTC - 900 方老师炸弹 —— 割点
- uestc 一个简单的迷宫问题
- uestc 方老师的分身IV
- 2014 UESTC Training for Search Algorithm Problem D 方老师与素数
- UESTC 898 方老师和缘分 --二分图匹配+强连通分量
- UESTC 方老师开橙卡
- uestc 方老师分身 I
- UESTC 899 方老师和农场 --双连通分量的构造