結果

問題 No.124 門松列(3)
ユーザー HachimoriHachimori
提出日時 2015-01-11 23:43:29
言語 Python2
(2.7.18)
結果
AC  
実行時間 147 ms / 5,000 ms
コード長 1,240 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 6,800 KB
実行使用メモリ 30,408 KB
最終ジャッジ日時 2023-09-03 23:43:22
合計ジャッジ時間 3,024 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
30,284 KB
testcase_01 AC 11 ms
6,060 KB
testcase_02 AC 12 ms
6,152 KB
testcase_03 AC 147 ms
30,392 KB
testcase_04 AC 136 ms
30,404 KB
testcase_05 AC 136 ms
30,408 KB
testcase_06 AC 11 ms
6,008 KB
testcase_07 AC 11 ms
6,004 KB
testcase_08 AC 11 ms
5,872 KB
testcase_09 AC 11 ms
6,004 KB
testcase_10 AC 11 ms
5,864 KB
testcase_11 AC 11 ms
6,120 KB
testcase_12 AC 11 ms
6,008 KB
testcase_13 AC 11 ms
6,008 KB
testcase_14 AC 12 ms
6,224 KB
testcase_15 AC 12 ms
6,224 KB
testcase_16 AC 20 ms
7,648 KB
testcase_17 AC 14 ms
6,548 KB
testcase_18 AC 12 ms
6,132 KB
testcase_19 AC 13 ms
6,512 KB
testcase_20 AC 16 ms
6,664 KB
testcase_21 AC 12 ms
6,240 KB
testcase_22 AC 15 ms
6,448 KB
testcase_23 AC 52 ms
14,564 KB
testcase_24 AC 72 ms
18,592 KB
testcase_25 AC 72 ms
18,668 KB
testcase_26 AC 32 ms
10,328 KB
testcase_27 AC 23 ms
8,368 KB
testcase_28 AC 135 ms
30,348 KB
testcase_29 AC 134 ms
30,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python


def read():
    col, row = map(int, raw_input().split())
    b = []
    for i in range(row):
        b.append(map(int, raw_input().split()))
    return row, col, b


def isKadomatsu(a, b, c):
    if a == 0 or b == 0:
        return True
    return b > a > c or b > c > a or a > c > b or c > a > b


def work((row, col, b)):
    # cost[r][c][A_0][A_1]: # of step
    cost = [[[[-1 for l in range(10)] for k in range(10)] for j in range(col)] for i in range(row)]
    Q = []
    
    cost[0][0][0][b[0][0]] = 0
    Q.append((0, 0, 0, b[0][0]))

    while Q:
        (r, c, A0, A1) = Q[0]
        del Q[0]

        if r == row - 1 and c == col - 1:
            print cost[r][c][A0][A1]
            return

        for (dr, dc) in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
            nr = r + dr
            nc = c + dc
            if not (0 <= nr < row and 0 <= nc < col):
                continue
            if not isKadomatsu(A0, A1, b[nr][nc]):
                continue
            if cost[nr][nc][A1][b[nr][nc]] != -1:
                continue
            cost[nr][nc][A1][b[nr][nc]] = cost[r][c][A0][A1] + 1
            Q.append((nr, nc, A1, b[nr][nc]))

    print -1

    
if __name__ == "__main__":
    work(read())
0