結果
問題 | No.124 門松列(3) |
ユーザー |
![]() |
提出日時 | 2020-09-13 12:40:06 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 71 ms / 5,000 ms |
コード長 | 942 bytes |
コンパイル時間 | 290 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 11,264 KB |
最終ジャッジ日時 | 2024-06-11 22:42:12 |
合計ジャッジ時間 | 2,502 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
import sysread=sys.stdin.buffer.readreadline=sys.stdin.buffer.readlinereadlines=sys.stdin.buffer.readlinesw, h=map(int, readline().split())m=list(map(int, read().split()))from collections import dequeque=deque()INF=10**9dp=[[INF]*(w*h) for _ in range(4)]dp[0][0]=0que.append((0, 0))dx=[1, -1, 0, 0]dy=[0, 0, 1, -1]def is_kadomatsu(a, b, c):if a!=c and ((a<b and b>c) or (a>b and b<c)):return Trueelse:return Falsewhile que:xy, k=que.popleft()x, y=divmod(xy, w)d=dp[k][xy]x2, y2=x-dx[k], y-dy[k]xy2=x2*w+y2for i, (a, b) in enumerate(zip(dx, dy)):x1, y1=x+a, y+bif x1<0 or x1>=h or y1<0 or y1>=w:continuexy1=x1*w+y1if dp[i][xy1]>d+1 and (d==0 or is_kadomatsu(m[xy2], m[xy], m[xy1])):dp[i][xy1]=d+1que.append((xy1, i))ans=min(dp[0][-1], dp[2][-1])if ans==INF:print(-1)else:print(ans)