結果
問題 | 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 = 0for x in X:for y in Y:if x == y:continuemp[(x, y)] = tt += 1edges = [[] for _ in range(t)]for x1 in X:for x2 in X:if x1 == x2:continuefor y in Y:if y in [x1, x2]:continueif x2 == 0:edges[mp[(x1, y)]].append((mp[(x2, y)], 1))elif x1 < y and y < x2:continueelif x1 > x2:continueelse:edges[mp[(x1, y)]].append((mp[(x2, y)], x2 - x1))X, Y = Y, Xfor x1 in X:for x2 in X:if x1 == x2:continuefor y in Y:if y in [x1, x2]:continueif x2 == 0:edges[mp[(y, x1)]].append((mp[(y, x2)], 1))elif x1 < y and y < x2:continueelif x1 > x2:continueelse:edges[mp[(y, x1)]].append((mp[(y, x2)], x2 - x1))dist = [1 << 60] * tdist[mp[(a, b)]] = 0hq = [(0, mp[(a, b)])]while hq:dd, pos = heappop(hq)if dist[pos] < dd:continuefor npos, cc in edges[pos]:if dist[npos] > dd + cc:dist[npos] = dd + ccheappush(hq, (dd + cc, npos))return dist[mp[(c, d)]]for i in range(int(input())):ans = solve(*map(int, input().split()))print(ans)