1096. 地牢大师

摘要
Title: 1096. 地牢大师
Tag: 三维BFS
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

1096. 地牢大师

  • 题意

    你现在被困在一个三维地牢中,需要找到最快脱离的出路!
    地牢由若干个单位立方体组成,其中部分不含岩石障碍可以直接通过,部分包含岩石障碍无法通过。
    向北,向南,向东,向西,向上或向下移动一个单元距离均需要一分钟。
    你不能沿对角线移动,迷宫边界都是坚硬的岩石,你不能走出边界范围。
    请问,你有可能逃脱吗?
    如果可以,需要多长时间?

  • 思路

    三维,照着写就行

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    '''
    Author: NEFU AB-IN
    Date: 2022-03-29 16:41:44
    FilePath: \ACM\Acwing\1096.py
    LastEditTime: 2022-03-29 16:54:14
    '''
    from collections import deque

    N = 110
    while True:
    st = [[[0] * N for _ in range(N)] for _ in range(N)]
    g = [[] for _ in range(N)]
    l, n, m = map(int, input().split())
    if l + n + m == 0:
    break
    for k in range(l):
    for i in range(n):
    g[k].append(list(input()))
    t = input()

    for k in range(l):
    for i in range(n):
    for j in range(m):
    if g[k][i][j] == 'S':
    sz, sx, sy = k, i, j
    if g[k][i][j] == 'E':
    ez, ex, ey = k, i, j

    dir = [[0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1], [1, 0, 0], [-1, 0, 0]]
    q = deque()
    q.appendleft([sz, sx, sy, 0])
    st[sz][sx][sy] = 1
    while q:
    z1, x1, y1, cnt = q.pop()
    if z1 == ez and x1 == ex and y1 == ey:
    print(f"Escaped in {cnt} minute(s).")
    break
    for i in range(6):
    z = z1 + dir[i][0]
    x = x1 + dir[i][1]
    y = y1 + dir[i][2]
    if x >= 0 and x < n and y >= 0 and y < m and z >= 0 and z < l and (
    g[z][x][y] == 'E'
    or g[z][x][y] == '.') and st[z][x][y] == 0:
    st[z][x][y] = 1
    q.appendleft([z, x, y, cnt + 1])
    if st[ez][ex][ey] == 0:
    print("Trapped!")
使用搜索:谷歌必应百度