首页 > 技术文章 > POJ2251 Dungeon Master

Lrixin 2021-01-18 21:11 原文

题目链接:https://vjudge.net/problem/POJ-2251
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!

题目大意:你被困在一个三维的空间中,现在要寻找最短路径逃生! 空间由立方体单位构成 你每次向上下前后左右移动一个单位需要一分钟 你不能对角线移动并且四周封闭 是否存在逃出生天的可能性?如果存在,则需要多少时间?

解题思路:参照二维迷宫做法,利用BFS寻找起点到终点的距离
Cpp Code

//POJ2251
#include <iostream>
#include<queue>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
#define maxn 31
char a[maxn][maxn][maxn];//地图
int d[maxn][maxn][maxn];//距离数组
int dir[6][3]={{-1,0,0},{1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int sum=0;
int L,R,C;
struct node{
int x,y,z;
};
node start,ed;//起点 终点
int  bfs()
{
    queue<node> Q;
    Q.push(start);
    node cur;
     node next;
    while(Q.size())
    {
        cur=Q.front();
        Q.pop();
            for(int i=0;i<6;i++)
            {
                next.x=cur.x+dir[i][0];
                next.y=cur.y+dir[i][1];
                next.z=cur.z+dir[i][2];
                if(next.x>=0&&next.x<R&&next.y>=0&&next.y<C&&next.z>=0&&next.z<L&&a[next.z][next.x][next.y]!='#'&&d[next.z][next.x][next.y]==0)
                {
                    d[next.z][next.x][next.y]=d[cur.z][cur.x][cur.y]+1;
                     if(next.x==ed.x&&next.y==ed.y&&next.z==ed.z)//到达终点时,马上退出
                          return d[ed.z][ed.x][ed.y];
                     Q.push(next);
                }
            }
    }
      return -1;
}
int main()
{
   while(cin>>L>>R>>C&&(L+R+C)!=0)
   {
       sum=0;
       memset(d,0,sizeof(d));
      for(int i=0;i<L;i++)
          for(int j=0;j<R;j++)
            for(int k=0;k<C;k++)
          {
 
           cin>>a[i][j][k];
           if(a[i][j][k]=='S')
           {
               start.z=i;start.x=j;start.y=k;
           }
           else if(a[i][j][k]=='E')
           {
               ed.z=i;ed.x=j;ed.y=k;
           }
       }
      if( bfs()==-1)
       printf("Trapped!\n");
      else
        printf("Escaped in %d minute(s).\n",d[ed.z][ed.x][ed.y]);
 
   }
    return 0;
}

Java Code



import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class node{
	int x,y,z,d;
	node(){}
	node(int x,int y,int z,int d){
		this.x=x;
		this.y=y;
		this.z=z;
		this.d=d;
	}
	
}


public class Main {
	static int L,R,C;
	static char[][][] dilao;
	static int sx,sy,sz,dx,dy,dz;
	static int[][] dir={{-1,0,0},{1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
	static boolean[][][] mark;
	
	static int bfs() {
		Queue<node> que=new LinkedList<node>();
		que.add(new node(sx,sy,sz,0));
		mark[sx][sy][sz]=true;
		while(!que.isEmpty()) {
			node fro=que.poll();
			if(fro.x==dx&&fro.y==dy&&fro.z==dz) {
				return fro.d;
			}
			for(int k=0;k<6;k++) {
				int nx=fro.x+dir[k][0];
				int ny=fro.y+dir[k][1];
				int nz=fro.z+dir[k][2];
				if(nx<0||nx>=L||ny<0||ny>=R||nz<0||nz>=C||mark[nx][ny][nz]||dilao[nx][ny][nz]=='#') {
					continue;
				}
				mark[nx][ny][nz]=true;
				que.offer(new node(nx,ny,nz,fro.d+1));
			}
		}
		return -1;
	}

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(true) {
			L=sc.nextInt();
			R=sc.nextInt();
			C=sc.nextInt();
			if(L==0&&R==0&&C==0) {
				break;
			}
			
			dilao=new char[L][R][C];
			mark=new boolean[L][R][C];
			String line;
			for(int i=0;i<L;i++) {
				for(int j=0;j<R;) {
					line=sc.nextLine();
					if(line==null||line.isEmpty()) {
						continue;
					}
		
					dilao[i][j]=line.toCharArray();
					for(int k=0;k<C;k++) {
						if(dilao[i][j][k]=='S') {
							sx=i;
							sy=j;
							sz=k;
						}
						else if(dilao[i][j][k]=='E') {
							dx=i;
							dy=j;
							dz=k;
						}
					}
					j++;
				}
				
			}
			int ans=bfs();
			if(ans==-1) {
				System.out.printf("Trapped!\n");
			}
			else {
				System.out.printf("Escaped in %d minute(s).\n",ans);
			}
		}
		
	}
}

推荐阅读