結果

問題 No.124 門松列(3)
ユーザー chocorusk
提出日時 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
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
readlines=sys.stdin.buffer.readlines

w, h=map(int, readline().split())
m=list(map(int, read().split()))
from collections import deque
que=deque()
INF=10**9
dp=[[INF]*(w*h) for _ in range(4)]
dp[0][0]=0
que.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 True
    else:
        return False
while 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+y2
    for i, (a, b) in enumerate(zip(dx, dy)):
        x1, y1=x+a, y+b
        if x1<0 or x1>=h or y1<0 or y1>=w:
            continue
        xy1=x1*w+y1
        if dp[i][xy1]>d+1 and (d==0 or is_kadomatsu(m[xy2], m[xy], m[xy1])):
            dp[i][xy1]=d+1
            que.append((xy1, i))
ans=min(dp[0][-1], dp[2][-1])
if ans==INF:
    print(-1)
else:
    print(ans)
0