結果

問題 No.424 立体迷路
ユーザー MitI_7MitI_7
提出日時 2016-10-02 15:11:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,590 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 10,984 KB
実行使用メモリ 9,264 KB
最終ジャッジ日時 2023-09-18 16:57:01
合計ジャッジ時間 1,906 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
9,044 KB
testcase_01 AC 24 ms
9,056 KB
testcase_02 AC 23 ms
9,044 KB
testcase_03 AC 23 ms
9,044 KB
testcase_04 AC 24 ms
9,064 KB
testcase_05 AC 24 ms
9,040 KB
testcase_06 AC 24 ms
9,088 KB
testcase_07 AC 24 ms
9,248 KB
testcase_08 AC 24 ms
9,024 KB
testcase_09 AC 24 ms
9,144 KB
testcase_10 AC 24 ms
9,060 KB
testcase_11 AC 25 ms
9,076 KB
testcase_12 AC 24 ms
9,056 KB
testcase_13 AC 25 ms
9,228 KB
testcase_14 AC 25 ms
9,228 KB
testcase_15 AC 24 ms
9,040 KB
testcase_16 AC 25 ms
9,180 KB
testcase_17 AC 25 ms
9,108 KB
testcase_18 AC 24 ms
9,052 KB
testcase_19 AC 25 ms
9,264 KB
testcase_20 AC 24 ms
9,116 KB
testcase_21 AC 24 ms
9,172 KB
testcase_22 AC 24 ms
9,172 KB
testcase_23 AC 24 ms
9,204 KB
testcase_24 AC 36 ms
9,084 KB
testcase_25 AC 37 ms
9,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue

dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def bfs(sy, sx, gy, gx, field):
    q = queue.Queue()
    q.put((sy, sx))

    used = [[False] * len(field[0]) for i in range(len(field))]
    used[sy][sx] = True

    while not q.empty():
        now_y, now_x = q.get(0)
        now_h = field[now_y][now_x]

        if (now_y, now_x) == (gy, gx):
            break

        for i in range(len(dy)):
            next_y, next_x = now_y + dy[i], now_x + dx[i]

            if 0 <= next_y < len(field) and 0 <= next_x < len(field[0]) and not used[next_y][next_x]:
                next_h = field[next_y][next_x]
                if abs(now_h - next_h) <= 1:
                    q.put((next_y, next_x))
                    used[next_y][next_x] = True

        for i in range(len(dy)):
            next_y, next_x = now_y + dy[i] * 2, now_x + dx[i] * 2
            m_y, m_x = now_y + dy[i], now_x + dx[i]

            if 0 <= next_y < len(field) and 0 <= next_x < len(field[0]) and not used[next_y][next_x]:
                next_h = field[next_y][next_x]
                m_h = field[m_y][m_x]
                if now_h == next_h and now_h >= m_h:
                    q.put((next_y, next_x))
                    used[next_y][next_x] = True

    return used[gy][gx]


def main():
    h, w = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    sx, sy, gx, gy = sx - 1, sy - 1, gx - 1, gy - 1
    field = []
    for _ in range(h):
        field.append(list(map(int, input())))

    print("YES" if bfs(sx, sy, gx, gy, field) else "NO")


if __name__ == '__main__':
    main()
0