結果

問題 No.124 門松列(3)
ユーザー ckawatakckawatak
提出日時 2019-01-20 13:33:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,043 ms / 5,000 ms
コード長 1,165 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 236,312 KB
最終ジャッジ日時 2023-10-11 05:23:57
合計ジャッジ時間 14,464 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,003 ms
235,452 KB
testcase_01 AC 175 ms
80,800 KB
testcase_02 AC 177 ms
80,808 KB
testcase_03 AC 1,043 ms
236,312 KB
testcase_04 AC 948 ms
232,708 KB
testcase_05 AC 967 ms
232,616 KB
testcase_06 AC 178 ms
80,808 KB
testcase_07 AC 177 ms
80,832 KB
testcase_08 AC 176 ms
80,620 KB
testcase_09 AC 178 ms
80,856 KB
testcase_10 AC 178 ms
80,952 KB
testcase_11 AC 175 ms
80,880 KB
testcase_12 AC 176 ms
80,740 KB
testcase_13 AC 178 ms
80,616 KB
testcase_14 AC 188 ms
82,004 KB
testcase_15 AC 192 ms
82,004 KB
testcase_16 AC 234 ms
85,636 KB
testcase_17 AC 193 ms
82,504 KB
testcase_18 AC 188 ms
82,240 KB
testcase_19 AC 191 ms
82,388 KB
testcase_20 AC 223 ms
84,764 KB
testcase_21 AC 190 ms
82,544 KB
testcase_22 AC 195 ms
82,240 KB
testcase_23 AC 327 ms
106,456 KB
testcase_24 AC 420 ms
125,364 KB
testcase_25 AC 509 ms
144,340 KB
testcase_26 AC 272 ms
97,060 KB
testcase_27 AC 230 ms
88,036 KB
testcase_28 AC 940 ms
232,648 KB
testcase_29 AC 942 ms
232,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from queue import PriorityQueue

W,H = list(map(int, input().split(' ')))

G = []
for i in range(H):
    G.append(list(map(int, input().split(' '))))

distance = []
for i in range(H):
    for j in range(W):
        distance.append([[float('inf') for _ in range(10)] for _ in range(W)])

def is_kadomatsu(prev,current,next):
    if prev == 0:
        return True
    return ((prev < current and next < current) or (current < prev and current < next)) and prev != next


def dijkstra():
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = PriorityQueue()

    que.put((0,0,0,0))
    distance[0][0][0] = 0

    answer = -1
    while not que.empty():
        dist,last,x,y = que.get()
        if x == H-1 and y == W-1:
            answer = dist
            break
        for i in range(len(dx)):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx and 0 <= ny and nx < H and ny < W \
               and distance[nx][ny][G[x][y]] == float('inf') \
                   and is_kadomatsu(last, G[x][y], G[nx][ny]):
                que.put((dist+1,G[x][y],nx,ny))
                distance[nx][ny][G[x][y]] = dist+1

    print(answer)

dijkstra()
0