結果
問題 | No.124 門松列(3) |
ユーザー | maspy |
提出日時 | 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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 269 ms
14,592 KB |
testcase_01 | AC | 33 ms
11,008 KB |
testcase_02 | AC | 33 ms
11,136 KB |
testcase_03 | AC | 312 ms
16,000 KB |
testcase_04 | AC | 312 ms
16,768 KB |
testcase_05 | AC | 312 ms
16,896 KB |
testcase_06 | AC | 33 ms
11,136 KB |
testcase_07 | AC | 32 ms
10,880 KB |
testcase_08 | AC | 33 ms
10,880 KB |
testcase_09 | AC | 31 ms
10,880 KB |
testcase_10 | AC | 33 ms
11,008 KB |
testcase_11 | AC | 33 ms
11,136 KB |
testcase_12 | AC | 31 ms
11,008 KB |
testcase_13 | AC | 32 ms
10,880 KB |
testcase_14 | AC | 34 ms
11,008 KB |
testcase_15 | AC | 34 ms
11,008 KB |
testcase_16 | AC | 54 ms
11,264 KB |
testcase_17 | AC | 37 ms
11,008 KB |
testcase_18 | AC | 34 ms
11,008 KB |
testcase_19 | AC | 37 ms
10,880 KB |
testcase_20 | AC | 43 ms
11,264 KB |
testcase_21 | AC | 34 ms
11,136 KB |
testcase_22 | AC | 40 ms
11,008 KB |
testcase_23 | AC | 130 ms
12,928 KB |
testcase_24 | AC | 180 ms
13,952 KB |
testcase_25 | AC | 178 ms
14,080 KB |
testcase_26 | AC | 82 ms
11,904 KB |
testcase_27 | AC | 63 ms
11,648 KB |
testcase_28 | AC | 317 ms
17,024 KB |
testcase_29 | AC | 315 ms
17,024 KB |
ソースコード
#!/usr/bin/ python3.8 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import itertools from collections import deque W, H = map(int, readline().split()) C = tuple(map(int, read().split())) dx = (1, 0, -1, 0) dy = (0, 1, 0, -1) N = H * W graph = [[] 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)): continue if not ((0 <= x2 < H) and (0 <= y2 < W)): continue i1 = x1 * W + y1 i2 = x2 * W + y2 n = C[i] n1 = C[i1] n2 = C[i2] if n1 == n2: continue if (n1 <= n <= n2) or (n1 >= n >= n2): continue v = 4 * i1 + j w = 4 * i + k graph[v].append(w) start = 4 * N goal = 4 * N + 1 graph[start] = [0, 1] graph[4 * (W * (H - 1) - 1)].append(goal) graph[4 * (H * W - 2) + 1].append(goal) INF = 4 * N + 100 dist = [INF] * (4 * N + 2) dist[start] = 0 q = deque([start]) while q: v = q.popleft() dv = dist[v] dw = dv + 1 for w in graph[v]: if dist[w] <= dw: continue dist[w] = dw q.append(w) answer = dist[goal] - 1 if answer > 4 * N + 2: answer = -1 print(answer)