結果

問題 No.124 門松列(3)
ユーザー roarisroaris
提出日時 2019-12-04 14:08:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,307 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 77,152 KB
最終ジャッジ日時 2024-05-07 20:00:07
合計ジャッジ時間 2,476 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
62,228 KB
testcase_01 WA -
testcase_02 AC 40 ms
54,540 KB
testcase_03 AC 76 ms
77,152 KB
testcase_04 AC 64 ms
73,812 KB
testcase_05 AC 45 ms
62,440 KB
testcase_06 AC 41 ms
54,748 KB
testcase_07 AC 38 ms
53,924 KB
testcase_08 AC 40 ms
55,092 KB
testcase_09 AC 40 ms
55,052 KB
testcase_10 AC 40 ms
55,096 KB
testcase_11 AC 40 ms
54,928 KB
testcase_12 AC 42 ms
54,880 KB
testcase_13 AC 41 ms
55,264 KB
testcase_14 AC 42 ms
54,992 KB
testcase_15 AC 41 ms
54,632 KB
testcase_16 WA -
testcase_17 AC 42 ms
55,760 KB
testcase_18 AC 40 ms
54,736 KB
testcase_19 AC 41 ms
54,484 KB
testcase_20 WA -
testcase_21 AC 40 ms
54,812 KB
testcase_22 AC 40 ms
55,496 KB
testcase_23 AC 46 ms
62,044 KB
testcase_24 AC 44 ms
62,584 KB
testcase_25 AC 45 ms
60,956 KB
testcase_26 AC 44 ms
60,892 KB
testcase_27 AC 50 ms
63,184 KB
testcase_28 AC 45 ms
61,736 KB
testcase_29 AC 46 ms
63,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def bfs():
    q = deque([])
    dist1 = [[10**18]*W for _ in range(H)] #↓↑
    dist2 = [[10**18]*W for _ in range(H)] #↑↓
    
    if M[0][0]>M[0][1]:
        q.append((0, 1, -1))
        dist2[0][1] = 1
    elif M[0][0]<M[0][1]:
        q.append((0, 1, 1))
        dist1[0][1] = 1
    
    if M[0][0]>M[1][0]:
        q.append((1, 0, -1))
        dist2[1][0] = 1
    elif M[0][0]<M[1][0]:
        q.append((1, 0, 1))
        dist1[1][0] = 1
    
    while q:
        cx, cy, flag = q.popleft()
        
        if flag==-1:
            for nx, ny in [(cx-1, cy), (cx+1, cy), (cx, cy-1), (cx, cy+1)]:
                if 0<=nx<H and 0<=ny<W and dist1[nx][ny]==10**18 and M[cx][cy]<M[nx][ny]:
                    dist1[nx][ny] = dist2[cx][cy]+1
                    q.append((nx, ny, 1))
        else:
            for nx, ny in [(cx-1, cy), (cx+1, cy), (cx, cy-1), (cx, cy+1)]:
                if 0<=nx<H and 0<=ny<W and dist2[nx][ny]==10**18 and M[cx][cy]>M[nx][ny]:
                    dist2[nx][ny] = dist1[cx][cy]+1
                    q.append((nx, ny, -1))
    
    return min(dist1[-1][-1], dist2[-1][-1])
    
W, H = map(int, input().split())
M = [list(map(int, input().split())) for _ in range(H)]
ans = bfs()

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