您的位置:首页 > 产品设计 > UI/UE

UESTC 方老师与迷宫

2014-11-01 23:55 267 查看
方老师被困在了一个3维的迷宫中,他很想逃离这个迷宫回去玩炉石传说,你能帮助他么?

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 InputSample Output
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0
Escaped in 11 minute(s).
Trapped!
3维bfs 六个方向搜就行了
#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;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: