結果
問題 | No.2213 Neq Move |
ユーザー | Shirotsume |
提出日時 | 2022-10-12 22:58:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 384 ms / 2,000 ms |
コード長 | 2,334 bytes |
コンパイル時間 | 139 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 79,264 KB |
最終ジャッジ日時 | 2024-07-07 15:07:14 |
合計ジャッジ時間 | 2,322 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 74 ms
69,504 KB |
testcase_01 | AC | 384 ms
79,264 KB |
testcase_02 | AC | 360 ms
78,752 KB |
testcase_03 | AC | 311 ms
78,636 KB |
testcase_04 | AC | 299 ms
78,736 KB |
testcase_05 | AC | 295 ms
78,704 KB |
ソースコード
from collections import deque import random import sys input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 def solve1(a, b, c, d): dist = [[inf] * 100 for _ in range(100)] dist[a][b] = 0 q = deque([(a, b)]) while q: i, j = q.popleft() if i == c and j == d: return dist[i][j] if i + 1 != j and i + 1 < 100 and dist[i + 1][j] > dist[i][j] + 1: dist[i + 1][j] = dist[i][j] + 1 q.append((i + 1, j)) if j + 1 != i and j + 1 < 100 and dist[i][j + 1] > dist[i][j] + 1: dist[i][j + 1] = dist[i][j] + 1 q.append((i, j + 1)) if j != 0 and dist[0][j] > dist[i][j] + 1: dist[0][j] = dist[i][j] + 1 q.append((0, j)) if i != 0 and dist[i][0] > dist[i][j] + 1: dist[i][0] = dist[i][j] + 1 q.append((i, 0)) def dijkstra(s, n, graph): INF = 10 ** 18 import heapq dist = [INF] * n dist[s] = 0 bef = [0] * n bef[s] = s hq = [(0, s)] heapq.heapify(hq) visit = [False] * n while hq: c, v = heapq.heappop(hq) visit[v] = True if c > dist[v]: continue for to, cost in graph[v]: if visit[to] == False and dist[v] + cost < dist[to]: dist[to] = cost + dist[v] bef[to] = v heapq.heappush(hq, (dist[to], to)) return dist, bef def solve2(a, b, c, d): ind = {} i = 0 for v in [0, 1, 2, a, b, c, d]: for u in [0, 1, 2, a, b, c, d]: if u != v: ind[u, v] = i i += 1 graph = [[] for _ in range(i)] for u1, v1 in ind.keys(): i1 = ind[u1, v1] for u2, v2 in ind.keys(): i2 = ind[u2, v2] if u1 <= u2 and v1 <= v2 and i1 != i2 and ((u1 < v1 and u2 < v2) or (u1 > v1 and u2 > v2)): graph[i1].append((i2, abs(u2 - u1) + abs(v2 - v1))) if (u2 == 0 and v1 == v2) or (v2 == 0 and u1 == u2): graph[i1].append((i2, 1)) D, _ = dijkstra(ind[a, b], i, graph) return (D[ind[(c, d)]]) for _ in range(ii()): a, b, c, d = mi() print(solve2(a, b, c, d))