結果
| 問題 |
No.2213 Neq Move
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-13 18:02:01 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 277 ms / 2,000 ms |
| コード長 | 1,755 bytes |
| コンパイル時間 | 433 ms |
| コンパイル使用メモリ | 82,196 KB |
| 実行使用メモリ | 78,636 KB |
| 最終ジャッジ日時 | 2024-07-16 11:27:18 |
| 合計ジャッジ時間 | 2,294 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 5 |
ソースコード
from heapq import *
def solve(a, b, c, d):
X = sorted({0, 1, 2, a, c})
Y = sorted({0, 1, 2, b, d})
mp = {}
t = 0
for x in X:
for y in Y:
if x == y:
continue
mp[(x, y)] = t
t += 1
edges = [[] for _ in range(t)]
for x1 in X:
for x2 in X:
if x1 == x2:
continue
for y in Y:
if y in [x1, x2]:
continue
if x2 == 0:
edges[mp[(x1, y)]].append((mp[(x2, y)], 1))
elif x1 < y and y < x2:
continue
elif x1 > x2:
continue
else:
edges[mp[(x1, y)]].append((mp[(x2, y)], x2 - x1))
X, Y = Y, X
for x1 in X:
for x2 in X:
if x1 == x2:
continue
for y in Y:
if y in [x1, x2]:
continue
if x2 == 0:
edges[mp[(y, x1)]].append((mp[(y, x2)], 1))
elif x1 < y and y < x2:
continue
elif x1 > x2:
continue
else:
edges[mp[(y, x1)]].append((mp[(y, x2)], x2 - x1))
dist = [1 << 60] * t
dist[mp[(a, b)]] = 0
hq = [(0, mp[(a, b)])]
while hq:
dd, pos = heappop(hq)
if dist[pos] < dd:
continue
for npos, cc in edges[pos]:
if dist[npos] > dd + cc:
dist[npos] = dd + cc
heappush(hq, (dd + cc, npos))
return dist[mp[(c, d)]]
for i in range(int(input())):
ans = solve(*map(int, input().split()))
print(ans)