結果
問題 | No.124 門松列(3) |
ユーザー |
![]() |
提出日時 | 2020-03-19 23:36:30 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 317 ms / 5,000 ms |
コード長 | 1,439 bytes |
コンパイル時間 | 316 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 17,024 KB |
最終ジャッジ日時 | 2024-12-14 03:39:56 |
合計ジャッジ時間 | 4,907 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#!/usr/bin/ python3.8# %%import sysread = sys.stdin.buffer.readreadline = sys.stdin.buffer.readlinereadlines = sys.stdin.buffer.readlinesimport itertoolsfrom collections import dequeW, H = map(int, readline().split())C = tuple(map(int, read().split()))dx = (1, 0, -1, 0)dy = (0, 1, 0, -1)N = H * Wgraph = [[] for _ in range(4 * N + 2)]for i in range(N):x, y = divmod(i, W)for j, k in itertools.product(range(4), repeat=2):x1 = x - dx[j]y1 = y - dy[j]x2 = x + dx[k]y2 = y + dy[k]if not ((0 <= x1 < H) and (0 <= y1 < W)):continueif not ((0 <= x2 < H) and (0 <= y2 < W)):continuei1 = x1 * W + y1i2 = x2 * W + y2n = C[i]n1 = C[i1]n2 = C[i2]if n1 == n2:continueif (n1 <= n <= n2) or (n1 >= n >= n2):continuev = 4 * i1 + jw = 4 * i + kgraph[v].append(w)start = 4 * Ngoal = 4 * N + 1graph[start] = [0, 1]graph[4 * (W * (H - 1) - 1)].append(goal)graph[4 * (H * W - 2) + 1].append(goal)INF = 4 * N + 100dist = [INF] * (4 * N + 2)dist[start] = 0q = deque([start])while q:v = q.popleft()dv = dist[v]dw = dv + 1for w in graph[v]:if dist[w] <= dw:continuedist[w] = dwq.append(w)answer = dist[goal] - 1if answer > 4 * N + 2:answer = -1print(answer)