結果

問題 No.124 門松列(3)
ユーザー ckawatakckawatak
提出日時 2019-01-20 00:00:10
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,154 bytes
コンパイル時間 1,139 ms
コンパイル使用メモリ 87,272 KB
実行使用メモリ 428,904 KB
最終ジャッジ日時 2023-09-25 15:26:51
合計ジャッジ時間 13,679 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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([[0 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 < W and ny < H \
               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