AcWing 1096. 地牢大师(三维bfs)

Problem

类似这样的最值bfs问题已经记住了模板,就是用queue来解决的bfs
和这一篇完全一样,就是扩展一下维度就可以了
二维的情况

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Queue;

class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(System.out);
    static int N = 110;
    static char g[][][] = new char[N][N][N];
    static int L, R, C;
    static int dx[] = new int[]{0, 0, 1, 0, -1, 0};
    static int dy[] = new int[]{1, -1, 0, 0, 0, 0};
    static int dz[] = new int[]{0, 0, 0, 1, 0, -1};
    static int dist[][][] = new int[N][N][N];

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

        while (true) {
            String s[] = br.readLine().split(" ");
            L = Integer.parseInt(s[0]);
            R = Integer.parseInt(s[1]);
            C = Integer.parseInt(s[2]);
            if (L == 0 || R == 0 || C == 0) break;
            Pair start = new Pair(-1, -1, -1), end = new Pair(-1, -1, -1);
            for (int i = 0; i < L; i++) {
                for (int j = 0; j < R; j++) {
                    String ss = br.readLine();
                    for (int k = 0; k < C; k++) {
                        g[i][j][k] = ss.charAt(k);
                        if (g[i][j][k] == 'S') start = new Pair(i, j, k);
                        else if (g[i][j][k] == 'E') end = new Pair(i, j, k);
                    }
                }
                br.readLine();
            }
            int res = bfs(start, end);
            if (res != -1) pw.println("Escaped in " + res + " minute(s).");
            else pw.println("Trapped!");
        }

        pw.flush();
        pw.close();
        br.close();

    }

    public static int bfs(Pair start, Pair end) {
        Queue<Pair> q = new ArrayDeque<>();
        for (int i = 0; i < L; i++) {
            for (int j = 0; j < R; j++)
                for (int k = 0; k < C; k++)
                    dist[i][j][k] = -1;
        }
        dist[start.L][start.R][start.C] = 0;
        q.add(start);
        while (!q.isEmpty()) {
            Pair t = q.poll();
            for (int i = 0; i < 6; i++) {
                int x = t.L + dx[i];
                int y = t.R + dy[i];
                int z = t.C + dz[i];
                if (x < 0 || x >= L || y < 0 || y >= R || z < 0 || z >= C) continue;
                if (dist[x][y][z] != -1) continue;
                if (g[x][y][z] == '#') continue;

                dist[x][y][z] = dist[t.L][t.R][t.C] + 1;
                if (x == end.L && y == end.R && z == end.C) return dist[x][y][z];
                q.offer(new Pair(x, y, z));
            }
        }
        return -1;
    }

}

class Pair {
    public int L, R, C;

    public Pair(int l, int r, int c) {
        L = l;
        R = r;
        C = c;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值