L

来源:互联网 发布:wan端口未连接 编辑:程序博客网 时间:2024/04/29 00:02

Description
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? 
Input
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.
Output
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!
Sample Input
3 4 5
S....
.###.
.##..
###.#


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


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


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


0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!

题意:在三维空间里从S到E要走几步,'.'的地方能走,'#'不能走,不能到达E输出trapped

解题思路:深搜广搜都很好做的一题,但深搜会超时,因为广搜时,按6个方向搜索,找到第一个到达E即为最小步数,直接推出就好,而深搜是找遍每种情况再比较找到最小结果

广度优先搜索:

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<queue>using namespace std;int l,r,c,cnt,flag,LL,RR,CC;char go[30][30][30];bool ok[30][30][30];int dl[6]={1,-1,0,0,0,0};int dr[6]={0,0,1,-1,0,0};int dc[6]={0,0,0,0,1,-1};struct node{int l,r,c,step;};bool work(int L,int R,int C){if(L>=0&&L<l&&R>=0&&R<r&&C>=0&&C<c)return true;return  false;}void BFS(){queue<node>Q;node t,tt; int ll,rr,cc;t.l=LL; t.r=RR; t.c=CC; t.step=0;Q.push(t);while(!Q.empty()) {t=Q.front(); Q.pop();if(go[t.l][t.r][t.c]=='E'){flag=1;cnt=t.step;return;}for(int i=0;i<6;i++){ll=t.l+dl[i];rr=t.r+dr[i];cc=t.c+dc[i];if(work(ll,rr,cc)&&ok[ll][rr][cc]==false&&(go[ll][rr][cc]=='.'||go[ll][rr][cc]=='E')){ok[ll][rr][cc]=true;tt.l=ll; tt.r=rr; tt.c=cc; tt.step=t.step+1;Q.push(tt);}}}}int main(){int i,j,k;while(scanf("%d%d%d",&l,&r,&c),l+r+c){memset(ok,false,sizeof(ok));string s;for(i=0;i<l;i++){for(j=0;j<r;j++){cin>>s;for(k=0;k<c;k++){if(s[k]=='S'){LL=i;RR=j;CC=k;}go[i][j][k]=s[k];}}}flag=cnt=0;ok[LL][RR][CC]=true;BFS();if(flag){ printf("Escaped in %d minute(s).\n",cnt);}elseprintf("Trapped!\n");}return 0;}
深度优先搜索(超时):

#include<iostream>#include<cstdio>#include<string>#include<cstring>using namespace std;int l,r,c,cnt,flag;char go[30][30][30];bool ok[30][30][30];int dl[6]={1,-1,0,0,0,0};int dr[6]={0,0,1,-1,0,0};int dc[6]={0,0,0,0,1,-1};bool work(int L,int R,int C){if(L>=0&&L<l&&R>=0&&R<r&&C>=0&&C<c)return true;return  false;}void DFS(int L,int R,int C,int step){if(go[L][R][C]=='E'){flag=1;if(cnt<step) cnt=step;return;}for(int i=0;i<6;i++){int ll=L+dl[i];int rr=R+dr[i];int cc=C+dc[i];if(work(ll,rr,cc)&&ok[ll][rr][cc]==false&&(go[ll][rr][cc]=='.'||go[ll][rr][cc]=='E')){ok[ll][rr][cc]=true;DFS(ll,rr,cc,step+1);ok[ll][rr][cc]=false;}}}int main(){int i,j,k,L,R,C;while(scanf("%d%d%d",&l,&r,&c),l+r+c){memset(ok,false,sizeof(ok));string s;for(i=0;i<l;i++){for(j=0;j<r;j++){cin>>s;for(k=0;k<c;k++){if(s[k]=='S'){L=i;R=j;C=k;}go[i][j][k]=s[k];}}}ok[L][R][C]=true;flag=cnt=0;DFS(L,R,C,0);if(flag){ printf("Escaped in %d minute(s).\n",cnt);}elseprintf("Trapped!\n");}return 0;}



原创粉丝点击