結果

問題 No.124 門松列(3)
ユーザー roarisroaris
提出日時 2019-12-09 09:17:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 148 ms / 5,000 ms
コード長 1,325 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 86,824 KB
実行使用メモリ 87,800 KB
最終ジャッジ日時 2023-09-02 14:23:16
合計ジャッジ時間 5,478 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
87,620 KB
testcase_01 AC 99 ms
71,604 KB
testcase_02 AC 96 ms
71,368 KB
testcase_03 AC 148 ms
87,800 KB
testcase_04 AC 114 ms
77,524 KB
testcase_05 AC 116 ms
77,272 KB
testcase_06 AC 99 ms
71,460 KB
testcase_07 AC 100 ms
71,456 KB
testcase_08 AC 98 ms
71,496 KB
testcase_09 AC 95 ms
71,748 KB
testcase_10 AC 96 ms
71,464 KB
testcase_11 AC 96 ms
71,704 KB
testcase_12 AC 97 ms
71,724 KB
testcase_13 AC 98 ms
71,340 KB
testcase_14 AC 95 ms
71,512 KB
testcase_15 AC 97 ms
71,552 KB
testcase_16 AC 115 ms
77,492 KB
testcase_17 AC 108 ms
76,732 KB
testcase_18 AC 104 ms
76,212 KB
testcase_19 AC 105 ms
76,692 KB
testcase_20 AC 113 ms
77,444 KB
testcase_21 AC 103 ms
76,120 KB
testcase_22 AC 104 ms
76,684 KB
testcase_23 AC 109 ms
77,704 KB
testcase_24 AC 111 ms
77,556 KB
testcase_25 AC 109 ms
77,548 KB
testcase_26 AC 109 ms
77,528 KB
testcase_27 AC 107 ms
77,136 KB
testcase_28 AC 112 ms
77,448 KB
testcase_29 AC 112 ms
77,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def bfs():
    dist = [[[[-1]*2 for _ in range(10)] for _ in range(W)] for _ in range(H)]
    q = deque([])
    
    if M[0][0]!=M[0][1]:
        flag = 1 if M[0][1]>M[0][0] else 0
        dist[0][1][M[0][0]][flag] = 1
        q.append((0, 1, M[0][1], M[0][0]))
    
    if M[0][0]!=M[1][0]:
        flag = 1 if M[1][0]>M[0][0] else 0
        dist[1][0][M[0][0]][flag] = 1
        q.append((1, 0, M[1][0], M[0][0]))
    
    while q:
        cx, cy, cur, prev = q.popleft()
        
        for nx, ny in [(cx-1, cy), (cx+1, cy), (cx, cy-1), (cx, cy+1)]:
            if not (0<=nx<H and 0<=ny<W):
                continue
            
            flag = 1 if M[nx][ny]>cur else 0
            flag2 = 1 if cur>prev else 0

            if M[nx][ny] not in [cur, prev] and flag^flag2==1 and dist[nx][ny][cur][flag]==-1:
                dist[nx][ny][cur][flag] = dist[cx][cy][prev][flag2]+1
                q.append((nx, ny, M[nx][ny], cur))
    
    return dist
            
W, H = map(int, input().split())
M = [list(map(int, input().split())) for _ in range(H)]
dist = bfs()
ans = 10**18

for i in range(10):
    for j in range(2):
        if dist[H-1][W-1][i][j]==-1:
            continue
        
        ans = min(ans, dist[H-1][W-1][i][j])

if ans==10**18:
    print(-1)
else:
    print(ans)
0