結果
問題 | No.124 門松列(3) |
ユーザー |
![]() |
提出日時 | 2019-12-09 09:17:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 81 ms / 5,000 ms |
コード長 | 1,325 bytes |
コンパイル時間 | 248 ms |
コンパイル使用メモリ | 82,688 KB |
実行使用メモリ | 82,760 KB |
最終ジャッジ日時 | 2024-06-11 21:01:18 |
合計ジャッジ時間 | 2,760 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
from collections import dequedef 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 0dist[0][1][M[0][0]][flag] = 1q.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 0dist[1][0][M[0][0]][flag] = 1q.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):continueflag = 1 if M[nx][ny]>cur else 0flag2 = 1 if cur>prev else 0if 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]+1q.append((nx, ny, M[nx][ny], cur))return distW, H = map(int, input().split())M = [list(map(int, input().split())) for _ in range(H)]dist = bfs()ans = 10**18for i in range(10):for j in range(2):if dist[H-1][W-1][i][j]==-1:continueans = min(ans, dist[H-1][W-1][i][j])if ans==10**18:print(-1)else:print(ans)