技巧
    BFS最短路
思路
    
实现
import java.io.*;
import java.util.LinkedList;

public class Main {
    static class Pos {
        char cheese;
        int step, level, row, col;

        public Pos(char cheese, int level, int row, int col) {
            this.cheese = cheese;
            this.level = level;
            this.row = row;
            this.col = col;
        }
    }

    static boolean checkPos(int n, int... pos) {
        for (int po : pos) {
            if (po >= n || po < 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        int n = Integer.parseInt(br.readLine());
        Pos[][][] cube = new Pos[n][n][n];
        for (int level = 0; level < n; level++) {
            for (int row = 0; row < n; row++) {
                char[] chars = br.readLine().toCharArray();
                for (int col = 0; col < n; col++) {
                    cube[level][row][col] = new Pos(chars[col], level, row, col);
                }
            }
        }
        int[][] direction = new int[][]{{1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}};
        LinkedList<Pos> queue = new LinkedList<>();
        queue.addLast(cube[0][0][0]);

        int ans = -1;
        out:
        while (!queue.isEmpty()) {
            Pos first = queue.removeFirst();
            for (int[] d : direction) {
                if (checkPos(n, first.level + d[0], first.row + d[1], first.col + d[2])) {
                    Pos target = cube[first.level + d[0]][first.row + d[1]][first.col + d[2]];
                    if (target.step == 0 && target.cheese != '*') {
                        target.step = first.step + 1;
                        if (first.level + d[0] == (n - 1) && first.row + d[1] == (n - 1) && first.col + d[2] == (n - 1)) {
                            ans = target.step + 1;
                            break out;
                        }
                        queue.addLast(target);
                    }
                }
            }
        }
        System.out.println(ans);
    }
}