結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  ckawatak | 
| 提出日時 | 2019-01-20 13:33:19 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 920 ms / 5,000 ms | 
| コード長 | 1,165 bytes | 
| コンパイル時間 | 171 ms | 
| コンパイル使用メモリ | 82,104 KB | 
| 実行使用メモリ | 230,892 KB | 
| 最終ジャッジ日時 | 2024-09-13 04:40:44 | 
| 合計ジャッジ時間 | 10,059 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
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()
            
            
            
        