結果
問題 | No.124 門松列(3) |
ユーザー | maspy |
提出日時 | 2020-03-19 23:36:30 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 309 ms / 5,000 ms |
コード長 | 1,439 bytes |
コンパイル時間 | 182 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 16,896 KB |
最終ジャッジ日時 | 2024-05-08 03:34:13 |
合計ジャッジ時間 | 4,561 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 257 ms
14,464 KB |
testcase_01 | AC | 33 ms
11,136 KB |
testcase_02 | AC | 29 ms
11,136 KB |
testcase_03 | AC | 297 ms
16,256 KB |
testcase_04 | AC | 298 ms
16,896 KB |
testcase_05 | AC | 308 ms
16,896 KB |
testcase_06 | AC | 28 ms
11,008 KB |
testcase_07 | AC | 30 ms
11,008 KB |
testcase_08 | AC | 30 ms
11,008 KB |
testcase_09 | AC | 31 ms
11,008 KB |
testcase_10 | AC | 30 ms
11,136 KB |
testcase_11 | AC | 31 ms
11,008 KB |
testcase_12 | AC | 31 ms
11,008 KB |
testcase_13 | AC | 32 ms
11,264 KB |
testcase_14 | AC | 32 ms
11,008 KB |
testcase_15 | AC | 31 ms
11,008 KB |
testcase_16 | AC | 50 ms
11,264 KB |
testcase_17 | AC | 37 ms
11,008 KB |
testcase_18 | AC | 34 ms
11,008 KB |
testcase_19 | AC | 36 ms
11,008 KB |
testcase_20 | AC | 41 ms
11,136 KB |
testcase_21 | AC | 34 ms
11,008 KB |
testcase_22 | AC | 37 ms
11,008 KB |
testcase_23 | AC | 125 ms
13,056 KB |
testcase_24 | AC | 177 ms
13,952 KB |
testcase_25 | AC | 175 ms
13,952 KB |
testcase_26 | AC | 78 ms
12,032 KB |
testcase_27 | AC | 58 ms
11,520 KB |
testcase_28 | AC | 302 ms
16,896 KB |
testcase_29 | AC | 309 ms
16,896 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)