結果
問題 |
No.124 門松列(3)
|
ユーザー |
|
提出日時 | 2019-12-09 09:35:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 66 ms / 5,000 ms |
コード長 | 1,096 bytes |
コンパイル時間 | 298 ms |
コンパイル使用メモリ | 82,524 KB |
実行使用メモリ | 77,592 KB |
最終ジャッジ日時 | 2024-06-11 21:54:19 |
合計ジャッジ時間 | 2,515 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
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)