POJ2251 Dungeon Master
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
三维迷宫最短路,BFS直接套板子即可。
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <cstring> #include <queue> using namespace std; int L,R,C; char mmap[35][35][35]; bool vis[35][35][35]={0}; int dir[6][3]={{-1,0,0},{1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//x y z struct point { int x; int y; int z; int cnt; }; point start,end; void bfs()//cnt统计步数 { memset(vis,0,sizeof(vis)); queue<point>q; point pre,nxt; vis[start.z][start.x][start.y]=1; start.cnt=0; q.push(start); while(!q.empty()) { pre=q.front(); q.pop(); if(pre.x==end.x&&pre.y==end.y&&pre.z==end.z) { cout<<"Escaped in "<<pre.cnt<<" minute(s)."<<endl; return; } int i; for(i=0;i<6;i++) { int nx=pre.x+dir[i][0]; int ny=pre.y+dir[i][1]; int nz=pre.z+dir[i][2]; if(nx>=0&&nx<R&&ny>=0&&ny<C&&nz>=0&&nz<L&&mmap[nz][nx][ny]!='#'&&!vis[nz][nx][ny])//也可以写成mmap[nz][nx][ny]=='.'||mmap[nz][nx][ny]=='E' 但不要只写 =='.' { nxt.x=nx; nxt.y=ny; nxt.z=nz; nxt.cnt=pre.cnt+1; q.push(nxt); vis[nz][nx][ny]=1; } } } cout<<"Trapped!"<<endl; } int main() { while(scanf("%d%d%d",&L,&R,&C)&&L&&R&&C) { int i,j,k; for(i=0;i<L;i++) { for(j=0;j<R;j++) { scanf("%s",&mmap[i][j]); } getchar(); getchar(); } for(i=0;i<L;i++) { for(j=0;j<R;j++) { for(k=0;k<C;k++) { if(mmap[i][j][k]=='S') { start.x=j; start.y=k; start.z=i; } else if(mmap[i][j][k]=='E') { end.x=j; end.y=k; end.z=i; } } } } bfs(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!