結果

問題 No.124 門松列(3)
ユーザー maspymaspy
提出日時 2020-03-19 23:36:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 293 ms / 5,000 ms
コード長 1,439 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 10,904 KB
実行使用メモリ 14,548 KB
最終ジャッジ日時 2023-08-20 21:14:09
合計ジャッジ時間 5,383 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 244 ms
12,004 KB
testcase_01 AC 20 ms
8,716 KB
testcase_02 AC 20 ms
8,592 KB
testcase_03 AC 285 ms
13,588 KB
testcase_04 AC 280 ms
14,400 KB
testcase_05 AC 280 ms
14,460 KB
testcase_06 AC 20 ms
8,788 KB
testcase_07 AC 20 ms
8,704 KB
testcase_08 AC 20 ms
8,564 KB
testcase_09 AC 19 ms
8,568 KB
testcase_10 AC 20 ms
8,796 KB
testcase_11 AC 20 ms
8,788 KB
testcase_12 AC 20 ms
8,592 KB
testcase_13 AC 19 ms
8,624 KB
testcase_14 AC 22 ms
8,776 KB
testcase_15 AC 23 ms
8,620 KB
testcase_16 AC 40 ms
9,016 KB
testcase_17 AC 25 ms
8,828 KB
testcase_18 AC 23 ms
8,816 KB
testcase_19 AC 24 ms
8,668 KB
testcase_20 AC 31 ms
8,744 KB
testcase_21 AC 23 ms
8,828 KB
testcase_22 AC 27 ms
8,912 KB
testcase_23 AC 116 ms
10,616 KB
testcase_24 AC 163 ms
11,776 KB
testcase_25 AC 159 ms
11,688 KB
testcase_26 AC 68 ms
9,788 KB
testcase_27 AC 49 ms
9,392 KB
testcase_28 AC 283 ms
14,372 KB
testcase_29 AC 293 ms
14,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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)
0