結果

問題 No.2928 Gridpath
ユーザー 👑 loop0919
提出日時 2024-09-13 01:13:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 1,089 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 78,868 KB
最終ジャッジ日時 2024-09-13 01:14:00
合計ジャッジ時間 3,671 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

ways = [(0, 1), (0, -1), (1, 0), (-1, 0)]

H, W = map(int, input().split())
S_i, S_j = map(lambda x: int(x) - 1, input().split())
G_i, G_j = map(lambda x: int(x) - 1, input().split())

grid = [[0 for _ in range(W)] for _ in range(H)]
stack = [(0, S_i, S_j)]  # 0: 行きがけ, 1: 戻りがけ
ans = 0


def next_hop(i, j):
    for di, dj in ways:
        next_i, next_j = i + di, j + dj
        if 0 <= next_i < H and 0 <= next_j < W:
            yield next_i, next_j


while stack:
    state, now_i, now_j = stack.pop()

    if now_i == G_i and now_j == G_j:
        ans += state
        continue

    if state == 0:
        grid[now_i][now_j] += 1

        for next_i, next_j in next_hop(now_i, now_j):
            grid[next_i][next_j] += 1

        for next_i, next_j in next_hop(now_i, now_j):
            if grid[next_i][next_j] <= 1:
                stack.append((1, next_i, next_j))
                stack.append((0, next_i, next_j))

    else:
        grid[now_i][now_j] -= 1

        for next_i, next_j in next_hop(now_i, now_j):
            grid[next_i][next_j] -= 1

print(ans)
0