为什么会直接re啊
查看原帖
为什么会直接re啊
997832
yahuac楼主2024/3/28 13:23

如题,求大佬解惑

import javax.swing.*;
import java.io.*;
import java.util.*;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static final int N = 300;
    static int layer, n, m;
    static char[][][] grip = new char[N][N][];
    static boolean[][][] visit = new boolean[N][N][N];

    public static void main(String[] args) throws IOException {

        while (true) {

            String[] initial = br.readLine().split(" ");

            layer = Integer.parseInt(initial[0]);
            n = Integer.parseInt(initial[1]);
            m = Integer.parseInt(initial[2]);

            if (layer == 0 && n == 0 && m == 0) {
                break;
            }
            int sl = -1, sx = -1, sy = -1;
            for (int i = 0; i < layer; i++) {
                for (int j = 0; j < n; j++) {
                    grip[i][j] = br.readLine().toCharArray();
                    Arrays.fill(visit[i][j], 0, m, false);

                    if (sl == -1) {
                        for (int k = 0; k < m; k++) {
                            if (grip[i][j][k] == 'S') {
                                sl = i;
                                sx = j;
                                sy = k;
                                break;
                            }
                        }
                    }
                }
            }
            if (sl == -1){
                System.out.println("Trapped!");
                continue;
            }
            int ans = bfs(sl, sx, sy);
            if (ans == -1) System.out.println("Trapped!");
            else System.out.printf("Escaped in %d minute(s).\n", ans);
        }

        br.close();
    }

    static Deque<int[]> deque = new LinkedList<>();
    static int[][] move = {
            {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1},
            {1, 0, 0}, {-1, 0, 0}
    };

    public static int bfs(int sl, int sx, int sy) {

        deque.clear();
        visit[sl][sx][sy] = true;
        deque.add(new int[]{sy, sx, sy, 0});

        while (!deque.isEmpty()) {
            int[] cur = deque.poll();
            for (int i = 0, nl, nx, ny; i < move.length; i++) {
                nl = cur[0] + move[i][0];
                nx = cur[1] + move[i][1];
                ny = cur[2] + move[i][2];

                if (nl >= 0 && nl < layer && nx >= 0 && ny >= 0 && nx < n && ny < m
                        && !visit[nl][nx][ny] && grip[nl][nx][ny] != '#'
                ) {
                    if (grip[nl][nx][ny] == 'E')  return cur[3] + 1;
                    visit[nl][nx][ny] = true;
                    deque.add(new int[]{nl, nx, ny, cur[3] + 1});
                }

            }
        }

        return -1;
    }

}

2024/3/28 13:23
加载中...