結果

問題 No.124 門松列(3)
ユーザー HachimoriHachimori
提出日時 2015-01-11 23:41:24
言語 Python2
(2.7.18)
結果
RE  
実行時間 -
コード長 1,240 bytes
コンパイル時間 642 ms
コンパイル使用メモリ 7,140 KB
実行使用メモリ 30,336 KB
最終ジャッジ日時 2023-09-03 23:42:54
合計ジャッジ時間 3,121 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
30,172 KB
testcase_01 AC 12 ms
5,892 KB
testcase_02 AC 12 ms
5,944 KB
testcase_03 AC 161 ms
30,304 KB
testcase_04 AC 143 ms
30,280 KB
testcase_05 AC 144 ms
30,176 KB
testcase_06 AC 12 ms
5,984 KB
testcase_07 AC 12 ms
6,004 KB
testcase_08 AC 12 ms
6,076 KB
testcase_09 AC 11 ms
6,008 KB
testcase_10 AC 11 ms
5,860 KB
testcase_11 AC 12 ms
5,868 KB
testcase_12 RE -
testcase_13 AC 11 ms
5,892 KB
testcase_14 AC 13 ms
6,100 KB
testcase_15 AC 13 ms
6,072 KB
testcase_16 RE -
testcase_17 WA -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 52 ms
14,392 KB
testcase_24 AC 72 ms
18,588 KB
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 AC 143 ms
30,336 KB
testcase_29 AC 143 ms
30,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python


def read():
    row, col = 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