結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-12-04 14:08:49 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,307 bytes | 
| コンパイル時間 | 170 ms | 
| コンパイル使用メモリ | 82,304 KB | 
| 実行使用メモリ | 76,672 KB | 
| 最終ジャッジ日時 | 2024-11-30 15:41:53 | 
| 合計ジャッジ時間 | 2,430 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 23 WA * 3 | 
ソースコード
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)
            
            
            
        