結果

問題 No.124 門松列(3)
ユーザー rpy3cpprpy3cpp
提出日時 2015-07-14 22:11:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 1,381 bytes
コンパイル時間 1,102 ms
コンパイル使用メモリ 10,948 KB
実行使用メモリ 9,516 KB
最終ジャッジ日時 2023-09-22 15:48:28
合計ジャッジ時間 2,873 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,500 KB
testcase_01 AC 15 ms
8,228 KB
testcase_02 AC 15 ms
8,364 KB
testcase_03 AC 29 ms
9,516 KB
testcase_04 AC 17 ms
8,272 KB
testcase_05 AC 17 ms
8,452 KB
testcase_06 AC 15 ms
8,328 KB
testcase_07 AC 15 ms
8,172 KB
testcase_08 AC 15 ms
8,312 KB
testcase_09 AC 15 ms
8,312 KB
testcase_10 AC 16 ms
8,220 KB
testcase_11 AC 15 ms
8,372 KB
testcase_12 AC 15 ms
8,188 KB
testcase_13 AC 15 ms
8,304 KB
testcase_14 AC 16 ms
8,276 KB
testcase_15 AC 16 ms
8,276 KB
testcase_16 AC 16 ms
8,320 KB
testcase_17 AC 16 ms
8,204 KB
testcase_18 AC 15 ms
8,332 KB
testcase_19 AC 16 ms
8,280 KB
testcase_20 AC 16 ms
8,184 KB
testcase_21 AC 15 ms
8,196 KB
testcase_22 AC 17 ms
8,336 KB
testcase_23 AC 16 ms
8,368 KB
testcase_24 AC 16 ms
8,276 KB
testcase_25 AC 16 ms
8,280 KB
testcase_26 AC 15 ms
8,312 KB
testcase_27 AC 15 ms
8,280 KB
testcase_28 AC 17 ms
8,372 KB
testcase_29 AC 17 ms
8,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    W, H = map(int, input().split())
    M = [0] * (W + 3)
    for row in range(H):
        M.extend(list(map(int, input().split())))
        M.extend([0, 0])
    M.extend([0] * (W + 1))
    return W, H, M

def solve(W, H, M):
    H2 = H + 2
    W2 = W + 2
    H1 = H + 1
    W1 = W + 1
    goal = H1 * W2 - 2
    start = W2 + 1
    frontiers = []
    if M[start + 1] != M[start]: frontiers.append((start + 1, start))
    if M[start + W2] != M[start]: frontiers.append((start + W2, start))
    visited = set(frontiers)
    dist = 1
    while frontiers:
        dist += 1
        new_frontiers = []
        for pos1, pos2 in frontiers:
            val1 = M[pos1]
            val2 = M[pos2]
            dif = val1 - val2
            for new_pos in [pos1-1, pos1+1, pos1-W2, pos1+W2]:
                h, w = divmod(new_pos, W2)
                if w == 0 or h == 0 or w == W1 or h == H1 or new_pos == pos2: continue
                if (new_pos, pos1) in visited: continue
                new_val = M[new_pos]
                if new_val == val1 or new_val == val2: continue
                if dif * (new_val - val1) > 0: continue
                new_frontiers.append((new_pos, pos1))
                visited.add((new_pos, pos1))
                if new_pos == goal: return dist
        frontiers = new_frontiers
    return -1

W, H, M = read_data()
print(solve(W, H, M))
0