結果

問題 No.3679 なんかでっかい虫リターンズ
コンテスト
ユーザー hotaru-ishibashi
提出日時 2026-09-08 14:50:23
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 56 ms / 2,000 ms
+ 638µs
コード長 833 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 66 ms
コンパイル使用メモリ 81,396 KB
実行使用メモリ 84,404 KB
最終ジャッジ日時 2026-09-08 14:50:28
合計ジャッジ時間 2,176 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
H, W = map(int, input().split())
A, B = map(int, input().split())
top, left, bottom, right = map(int, input().split())
P, Q = map(int, input().split())
df = [[-1, 0],[0, -1], [1, 0], [0, 1]]
def bfs(x, y):
    dists = [[-1] * (W) for _ in range(H)]
    dists[x][y] = 0
    que = deque([[x, y]])

    while que:
        cx, cy = que.popleft()
        for dx, dy in df:
            nx, ny = cx+dx, cy+dy
            if nx < 0 or nx >= H or ny < 0 or ny >= W: continue
            if dists[nx][ny] != -1: continue
            dists[nx][ny] = dists[cx][cy]+1
            que.append([nx, ny])
    return dists

d1 = bfs(A-1, B-1)
d2 = bfs(P-1, Q-1)
d = abs(A-P) + abs(B-Q)
ans = 10**18
for i in range(top-1, bottom):
    for j in range(left-1, right):
        ans = min(ans, d1[i][j] + d2[i][j]+d)
print(ans)
0