353 3D dungeon【bfs】

来源:互联网 发布:装企叁度erp软件 编辑:程序博客网 时间:2024/04/27 23:17

3D dungeon

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. 

Is an escape possible? If yes, how long will it take? 
输入
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
输出
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, print the line 
Trapped!
样例输入
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
样例输出
Escaped in 11 minute(s).Trapped!



bfs 求最短路径问题,前两天刚做过一遍,今天又写了一篇,

突然感觉上次代码写的有问题,不过竟然两次都能ac,搞不明白到底是怎么了,问题在下标的控制上,上次好像是坐标控制错了,但是竟然没有判错,这一次感觉坐标是对的了,不过纠结了,C语言里的三维数组来模拟立体的坐标,这样真的很不方便,因为数组的下标是靠后的维数变化最快,但是坐标的话,三个变量是独立变化的,额,搞不明白了,反正感觉这个题有问题,也或是自己理解有误.....



#include<stdio.h>#include<string.h>#include<queue>using namespace std;int l,r,c,vis[55][55][55];char x[55][55][55];int kase,fx[2]={-1,1},fy[4]={0,-1,1,0},fz[4]={-1,0,0,1};struct migong{int x,y,z;int t;}st,tp;void bfs(){queue<migong>q;q.push(st);//入队首元素while(!q.empty()){st=q.front();q.pop();if(x[st.x][st.y][st.z]=='E'){kase=1;return;}for(int i=0;i<2;++i){tp=st;tp.x+=fx[i];//x 是第一维,也就是控制的第几层if(tp.x<0||tp.x>=l||x[tp.x][tp.y][tp.z]=='#'){continue;}if(!vis[tp.x][tp.y][tp.z]){++tp.t;vis[tp.x][tp.y][tp.z]=1;q.push(tp);}}for(int i=0;i<4;++i){tp=st;tp.y+=fy[i];tp.z+=fz[i];// y , z 是两外两个维,控制的同一层的前后左右if(tp.y<0||tp.y>=r||tp.z<0||tp.z>=c||x[tp.x][tp.y][tp.z]=='#'){continue;}if(!vis[tp.x][tp.y][tp.z]){++tp.t;vis[tp.x][tp.y][tp.z]=1;q.push(tp);}}}}int main(){int i,j,k,u,v,w;char y;while(scanf("%d%d%d",&l,&r,&c),l|r|c){for(i=0;i<l;++i){for(j=0;j<r;++j){getchar();for(k=0;k<c;++k){scanf("%c",&y);x[i][j][k]=y;if(y=='S')//标记起点位置{u=i;v=j;w=k;}}}getchar();}memset(vis,0,sizeof(vis));kase=0;vis[u][v][w]=1;//标记 st.x=u;st.y=v;st.z=w;st.t=0;bfs();if(kase==1)//对应输出..{printf("Escaped in %d minute(s).\n",st.t);continue;}printf("Trapped!\n");}}



0 0
原创粉丝点击