Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48605 Accepted: 18339 Description You are trapped in a 3D dungeon and need t ...
Dungeon Master
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 formEscaped 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! Source Ulm Local 1997 |
題意:給你一個多維的迷宮,問能不能可以從起點走到終點,可以上下穿梭層數,也可以東南西北走,都是一秒一個單位,可以到終點就輸出步數,不可以就輸出那句話
思路:其實和普通的二維迷宮差不多,就是多了一個層數,也就是多了兩個方向(上下層數)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> #include<queue> #include<set> #include<vector> using namespace std; #define INF 0x3f3f3f3f #define eps 1e-10 #define PI acos(-1.0) #define ll long long int const maxn = 35; const int mod = 1e9 + 7; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } char map[maxn][maxn][maxn]; int vis[maxn][maxn][maxn]; int k,n,m,sx,sy,sz,ex,ey,ez; int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; struct node { int x,y,z,step; }; int check (int x,int y,int z) { if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m) return 1; else if(map[x][y][z]=='#') return 1; else if(vis[x][y][z]) return 1; return 0; } int bfs() { node a,next; queue<node>que; a.x=sx; a.y=sy; a.z=sz; a.step=0; vis[sx][sy][sz]=1; que.push(a); while(!que.empty()) { a=que.front(); que.pop(); if(a.x == ex && a.y == ey && a.z == ez) return a.step; for(int i=0;i<6;i++) { next=a; next.x=a.x+dir[i][0]; next.y=a.y+dir[i][1]; next.z=a.z+dir[i][2]; if(check(next.x,next.y,next.z)) continue; vis[next.x][next.y][next.z]=1; next.step=a.step+1; que.push(next); } } return 0; } int main() { while(~scanf("%d %d %d",&k,&n,&m)) { if(k==0 && n==0 && m==0) break; for(int i=0;i<k;i++) { for(int j=0;j<n;j++) { scanf("%s",map[i][j]); for(int r=0;r<m;r++) { if(map[i][j][r]=='S') { sx=i;sy=j;sz=r; } else if(map[i][j][r]=='E') { ex=i;ey=j;ez=r; } } } } memset(vis,0,sizeof(vis)); int ans; ans=bfs(); if(ans) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0; }