結果
| 問題 |
No.124 門松列(3)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-12-04 14:19:41 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,426 bytes |
| コンパイル時間 | 1,316 ms |
| コンパイル使用メモリ | 81,792 KB |
| 実行使用メモリ | 76,672 KB |
| 最終ジャッジ日時 | 2024-11-30 15:41:50 |
| 合計ジャッジ時間 | 3,037 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 24 WA * 2 |
ソースコード
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, M[0][0]))
dist2[0][1] = 1
elif M[0][0]<M[0][1]:
q.append((0, 1, 1, M[0][0]))
dist1[0][1] = 1
if M[0][0]>M[1][0]:
q.append((1, 0, -1, M[0][0]))
dist2[1][0] = 1
elif M[0][0]<M[1][0]:
q.append((1, 0, 1, M[0][0]))
dist1[1][0] = 1
while q:
cx, cy, flag, prev_prev = 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] and M[nx][ny]!=prev_prev:
dist1[nx][ny] = dist2[cx][cy]+1
q.append((nx, ny, 1, M[cx][cy]))
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] and M[nx][ny]!=prev_prev:
dist2[nx][ny] = dist1[cx][cy]+1
q.append((nx, ny, -1, M[cx][cy]))
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)