結果

問題 No.124 門松列(3)
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-03-12 13:50:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 83 ms / 5,000 ms
コード長 1,121 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 81,836 KB
実行使用メモリ 77,396 KB
最終ジャッジ日時 2023-10-18 10:36:13
合計ジャッジ時間 3,576 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
72,932 KB
testcase_01 AC 47 ms
55,712 KB
testcase_02 AC 48 ms
55,712 KB
testcase_03 AC 83 ms
77,396 KB
testcase_04 AC 52 ms
63,732 KB
testcase_05 AC 52 ms
63,732 KB
testcase_06 AC 47 ms
55,712 KB
testcase_07 AC 48 ms
55,712 KB
testcase_08 AC 47 ms
55,712 KB
testcase_09 AC 48 ms
55,712 KB
testcase_10 AC 49 ms
55,712 KB
testcase_11 AC 47 ms
55,712 KB
testcase_12 AC 47 ms
55,712 KB
testcase_13 AC 48 ms
55,712 KB
testcase_14 AC 49 ms
57,760 KB
testcase_15 AC 48 ms
57,760 KB
testcase_16 AC 61 ms
68,460 KB
testcase_17 AC 51 ms
57,760 KB
testcase_18 AC 49 ms
57,760 KB
testcase_19 AC 50 ms
57,760 KB
testcase_20 AC 59 ms
66,244 KB
testcase_21 AC 49 ms
57,760 KB
testcase_22 AC 50 ms
57,760 KB
testcase_23 AC 52 ms
63,732 KB
testcase_24 AC 52 ms
63,732 KB
testcase_25 AC 52 ms
63,732 KB
testcase_26 AC 50 ms
61,684 KB
testcase_27 AC 50 ms
61,684 KB
testcase_28 AC 52 ms
63,732 KB
testcase_29 AC 52 ms
63,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

W,H = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
INF = (1<<30)
dp = [[[INF]*W for i in range(H)] for j in range(10)]

v = deque()
dp[A[0][0]][0][1]=1
dp[A[0][0]][1][0]=1
v.append((0,1,A[0][0]))
v.append((1,0,A[0][0]))
def nb(x,y):
    tmp = []
    if x+1<H:
        tmp.append((x+1,y))
    if x-1>=0:
        tmp.append((x-1,y))
    if y+1<W:
        tmp.append((x,y+1))
    if y-1>=0:
        tmp.append((x,y-1))
    return tmp
while v:
    
    x,y,b = v.popleft()
    
    a = A[x][y]
    base = dp[b][x][y]
    
    for ix,iy in nb(x,y):
        
        c = A[ix][iy]
        
        X = [b,a,c]
        Y = X[:]
        Y.sort()
        if len(set(Y))!=3:
            continue
        if Y[1]!=X[1]:
            
            if dp[a][ix][iy]!=INF:
                continue
            dp[a][ix][iy] = base + 1
            v.append((ix,iy,a))

ans = min(dp[i][-1][-1] for i in range(10))
if ans==INF:
    print(-1)
else:
    print(ans)
    
0