結果

問題 No.124 門松列(3)
ユーザー roarisroaris
提出日時 2019-12-09 09:35:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 124 ms / 5,000 ms
コード長 1,096 bytes
コンパイル時間 490 ms
コンパイル使用メモリ 87,140 KB
実行使用メモリ 77,864 KB
最終ジャッジ日時 2023-09-02 15:19:47
合計ジャッジ時間 5,164 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
77,788 KB
testcase_01 AC 98 ms
71,688 KB
testcase_02 AC 96 ms
71,764 KB
testcase_03 AC 124 ms
77,864 KB
testcase_04 AC 100 ms
77,256 KB
testcase_05 AC 104 ms
77,084 KB
testcase_06 AC 90 ms
71,588 KB
testcase_07 AC 96 ms
71,580 KB
testcase_08 AC 94 ms
71,336 KB
testcase_09 AC 96 ms
71,580 KB
testcase_10 AC 93 ms
71,620 KB
testcase_11 AC 91 ms
71,688 KB
testcase_12 AC 92 ms
71,476 KB
testcase_13 AC 95 ms
71,664 KB
testcase_14 AC 96 ms
71,740 KB
testcase_15 AC 96 ms
71,724 KB
testcase_16 AC 109 ms
77,132 KB
testcase_17 AC 96 ms
71,808 KB
testcase_18 AC 97 ms
71,556 KB
testcase_19 AC 96 ms
71,344 KB
testcase_20 AC 108 ms
77,468 KB
testcase_21 AC 96 ms
71,744 KB
testcase_22 AC 98 ms
71,748 KB
testcase_23 AC 104 ms
77,248 KB
testcase_24 AC 102 ms
77,152 KB
testcase_25 AC 102 ms
77,312 KB
testcase_26 AC 101 ms
77,276 KB
testcase_27 AC 102 ms
77,212 KB
testcase_28 AC 104 ms
77,128 KB
testcase_29 AC 104 ms
77,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def bfs():
    dist = [[[-1]*4 for _ in range(W)] for _ in range(H)]
    q = deque([])
    
    if M[0][0]!=M[0][1]:
        dist[0][1][0] = 1
        q.append((0, 1, 0, 0, 0))
    
    if M[0][0]!=M[1][0]:
        dist[1][0][1] = 1
        q.append((1, 0, 0, 0, 1))
        
    while q:
        cx, cy, px, py, f = q.popleft()
        
        for nx, ny, nf in [(cx, cy-1, 0), (cx-1, cy, 1), (cx, cy+1, 2), (cx+1, cy, 3)]:
            if not (0<=nx<H and 0<=ny<W):
                continue
            
            a, b, c = M[nx][ny], M[cx][cy], M[px][py]
            
            if dist[nx][ny][nf]==-1 and a not in [b, c] and ((a>b and b<c) or (a<b and b>c)):
                dist[nx][ny][nf] = dist[cx][cy][f]+1
                q.append((nx, ny, cx, cy, nf))
    
    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(4):
    if dist[H-1][W-1][i]==-1:
        continue
    
    ans = min(ans, dist[H-1][W-1][i])

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