結果

問題 No.424 立体迷路
ユーザー MitI_7MitI_7
提出日時 2016-10-02 15:04:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,586 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-05-01 09:36:05
合計ジャッジ時間 1,864 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 29 ms
11,264 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 29 ms
11,264 KB
testcase_09 AC 34 ms
11,264 KB
testcase_10 AC 28 ms
11,392 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 31 ms
11,264 KB
testcase_15 AC 31 ms
11,392 KB
testcase_16 WA -
testcase_17 RE -
testcase_18 AC 33 ms
11,264 KB
testcase_19 AC 33 ms
11,264 KB
testcase_20 AC 30 ms
11,264 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
権限があれば一括ダウンロードができます

ソースコード

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) == (gx, gy):
            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]
            m_h = field[m_y][m_x]

            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 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