3D迷宫(啊啊啊我要预习)java版

Juruo is an adventurous person. Now he is trapped in a perplexing 3D maze. Due to the air is damp crisp, he has to find a quickest way out. The 3D maze is made up of unit cubes. Some cubes are filled with snakes while others are not. Snakes!!! It will take one minute to take a unit move. He can choose to go north, south, east, west, up or down, but he cannot move diagonally. 

Is an escape possible? If yes, how long will it take? 

Input

The input contains a number of 3D mazes. Each description of maze starts with a line containing three integers L, R and C (L, R, C <= 30). 
L: The number of levels makes up the maze. 
R and C: The number of rows and columns maks up the plane of each level. 
Then there will follow L blocks of R lines, and each containing C characters. Each character describes one cube of the maze. A cube full of snakes is indicated by a '#' and empty cubes 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. The Input terminates when L = R = C = 0.

Output

Each maze generates one line of output. If it is possible to reach the exit, output 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, output 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!

Hint

BFS

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

class Node {
    int x,y,z;
    int step;
    Node() {}
    Node(int z,int y,int x,int step) {
        this.z = z;
        this.y = y;
        this.x = x;
        this.step = step;
    }
    Node(int z,int y,int x) {
        this.z = z;
        this.y = y;
        this.x = x;
    }
}
public class Main {
    static int high,row,cloum;//高,行,列
    static int[][][] map;//0表示能走,1表示不能走
    static boolean[][][] vis;
    static Queue<Node> q;
    static Node start,end;
    static int[][] move = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
    static boolean check(int x,int y,int z) {
        return x >= 0 && x < cloum && y >= 0 && y < row && z >= 0 && z < high && map[z][y][x] != 1 && !vis[z][y][x];
    }
    
    static void bfs() {
        Node cur,new_cur;
        vis[start.z][start.y][start.x] = true;
        q = new LinkedList<Node>();
        q.add(start);
        while(!q.isEmpty()) {
            cur = new Node(q.peek().z,q.peek().y,q.peek().x,q.poll().step);
            if(cur.x == end.x && cur.y == end.y && cur.z == end.z) {
                System.out.println("Escaped in " + cur.step + " minute(s).");
                return;
            }
            for(int i = 0;i < 6;i++) {
                new_cur = new Node(cur.z,cur.y,cur.x,cur.step);
                new_cur.x += move[i][0];
                new_cur.y += move[i][1];
                new_cur.z += move[i][2];
                if(check(new_cur.x,new_cur.y,new_cur.z)) {
                    vis[new_cur.z][new_cur.y][new_cur.x] = true;
                    new_cur.step++;
                    q.add(new_cur);
                }
            }
        }
        System.out.println("Trapped!");
    }
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
            high = cin.nextInt();
            row = cin.nextInt();
            cloum = cin.nextInt();
            vis = new boolean[high][row][cloum];
            if(high == 0)
                break;
            map = new int[high][row][cloum];
            String tmp;
            for(int z = 0;z < high;z++) {//高
                for(int y = 0;y < row;y++) {//行
                    tmp = cin.next();
                    for(int x = 0;x < cloum;x++) {//列
                        if(tmp.charAt(x) == 'S') 
                            start = new Node(z,y,x,0);
                        else if(tmp.charAt(x) == 'E') 
                            end = new Node(z,y,x);
                        else if(tmp.charAt(x) == '#')
                            map[z][y][x] = 1;
                    }
                }
            }
            bfs();
        }
    }
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-19 14:35
点赞 评论 收藏
分享
05-12 17:00
门头沟学院 Java
king122:你的项目描述至少要分点呀,要实习的话,你的描述可以使用什么技术,实现了什么难点,达成了哪些数字指标,这个数字指标尽量是真实的,这样面试应该会多很多,就这样自己包装一下,包装不好可以找我,我有几个大厂最近做过的实习项目也可以包装一下
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-19 17:02
鼠鼠深知pdd的强度很大,但是现在没有大厂offer,只有一些不知名小厂我是拒绝等秋招呢,还是接下?求大家帮忙判断一下!
水中水之下水道的鼠鼠:接了再说,不图转正的话混个实习经历也不错
投递拼多多集团-PDD等公司10个岗位 >
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务