POJ2251:Dungeon Master(BFS)

本文介绍了一种基于BFS的3D迷宫逃脱算法,通过三维数组模拟迷宫结构,实现从起点到出口的最短路径寻找。文章详细解释了算法流程,包括如何处理六向移动和判断路径有效性。

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是出口,'#'代表的是墙, '.' 代表的是路,问由S到E所要经过的最短时间。

      BFS题目。该题目和迷宫不同的是,迷宫是平面上的东南西北的移动,相当于在大楼里面的一层中找出口,而该题目在迷宫基础上增加了上下的移动,即在大楼里的上下层之间的移动,需要建立三维数组,找到S的位置,移动方向也由原来的四个增加到6个,知道找到出口E为止,若找遍所有的能走的地方没有找到出口E,则就出不来了。

代码如下:

public class BTest {

    //移动方向,前后左右上下六个方向坐标
    private static final int[][] dir = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
    private static volatile int time = -1;
    //存储输入的内容
    private static final char[][][] arr = new char[40][40][40];
    //记录访问过的坐标
    private static final int[][][] visited = new int[40][40][40];
    private static int L, R, C, sx, sy, sz, ex, ey, ez;
    //队列
    private static final LinkedList<String> list = new LinkedList<String>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        String[] numStr = scanner.nextLine().split(" ");
        L = Integer.valueOf(numStr[0]);
        R = Integer.valueOf(numStr[1]);
        C = Integer.valueOf(numStr[2]);
        for (int i = 0; i < L; i++){
            for (int j = 0; j < R; j++){
                String[] tmp = scanner.nextLine().split(" ");
                for (int k = 0; k < C; k++){
                    arr[i][j][k] = tmp[k].toCharArray()[0];
                    if (arr[i][j][k] == 'S'){
                        sx = i;
                        sy = j;
                        sz = k;
                    }
                    if (arr[i][j][k] == 'E'){
                        ex = i;
                        ey = j;
                        ez = k;
                    }
                }
            }
        }
        scanner.close();
        BFSSearch();
    }

    private static void BFSSearch(){
        //标记记录已经被访问过
        visited[sx][sy][sz] = 1;
        //入队
        list.add(sx + "|" + sy + "|" + sz);
        int l, r, c;
        while (!list.isEmpty()){
            String[] tmp = list.getFirst().split("|");
            list.removeFirst();
            l = Integer.parseInt(tmp[0]);
            r = Integer.parseInt(tmp[1]);
            c = Integer.parseInt(tmp[2]);
            if (l == ex && r == ey && c == ez){
                System.out.println("Escaped in " + time + " minute!");
                return;
            }
            //遍历六个方向
            for (int i = 0; i < 6; i++){
                l += dir[i][0];
                r += dir[i][1];
                c += dir[i][2];
                if (!judge(r, c, l)){
                    continue;
                }
                time++;
                if (visited[l][r][c] == 0){
                    visited[l][r][c] = 1;
                    list.add(l + "|" + r + "|" + c);
                }
            }
        }
        System.out.println("Trapped!");
    }

    private static boolean judge(int r, int c, int l){
        if (r >= 0 && r < R && c >= 0 && c < C && l >= 0 && l < L
                && arr[l][r][c] != '#') return true;
        return false;
    }

}

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值