結果

問題 No.124 門松列(3)
ユーザー rlangevinrlangevin
提出日時 2023-09-13 12:07:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 155 ms / 5,000 ms
コード長 1,099 bytes
コンパイル時間 958 ms
コンパイル使用メモリ 86,948 KB
実行使用メモリ 83,320 KB
最終ジャッジ日時 2023-09-13 12:07:47
合計ジャッジ時間 5,831 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
78,000 KB
testcase_01 AC 99 ms
71,744 KB
testcase_02 AC 99 ms
71,660 KB
testcase_03 AC 155 ms
83,320 KB
testcase_04 AC 107 ms
77,208 KB
testcase_05 AC 106 ms
77,012 KB
testcase_06 AC 99 ms
71,476 KB
testcase_07 AC 100 ms
71,292 KB
testcase_08 AC 101 ms
71,540 KB
testcase_09 AC 103 ms
71,452 KB
testcase_10 AC 97 ms
71,292 KB
testcase_11 AC 96 ms
71,504 KB
testcase_12 AC 94 ms
71,484 KB
testcase_13 AC 97 ms
71,680 KB
testcase_14 AC 101 ms
76,228 KB
testcase_15 AC 104 ms
76,144 KB
testcase_16 AC 122 ms
77,728 KB
testcase_17 AC 105 ms
76,152 KB
testcase_18 AC 107 ms
76,228 KB
testcase_19 AC 106 ms
76,144 KB
testcase_20 AC 120 ms
77,756 KB
testcase_21 AC 105 ms
75,948 KB
testcase_22 AC 105 ms
75,988 KB
testcase_23 AC 106 ms
76,708 KB
testcase_24 AC 106 ms
77,196 KB
testcase_25 AC 105 ms
76,852 KB
testcase_26 AC 104 ms
76,668 KB
testcase_27 AC 101 ms
76,828 KB
testcase_28 AC 109 ms
76,800 KB
testcase_29 AC 107 ms
77,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *

W, H = map(int, input().split())
M = []
for i in range(H):
    M.append(list(map(int, input().split())))

inf = 10 ** 18
dist = defaultdict(lambda:inf)
Q = deque()
if M[1][0] != M[0][0]:
    dist[(1, 0, 0, 0)] = 1
    Q.append([1, 0, 0, 0])
if M[0][1] != M[0][0]:
    dist[(0, 1, 0, 0)] = 1
    Q.append([0, 1, 0, 0])


def kadomatsu(a, b, c):
    S = set([a, b, c])
    if len(S) != 3:
        return False
    if b > a and b > c:
        return True
    if b < a and b < c:
        return True
    return False

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
while Q:
    x, y, px, py = Q.popleft()
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
        if nx < 0 or nx > H - 1 or ny < 0 or ny > W - 1:
            continue
        if dist[(nx, ny, x, y)] != inf:
            continue
        if kadomatsu(M[px][py], M[x][y], M[nx][ny]):
            dist[(nx, ny, x, y)] = dist[(x, y, px, py)] + 1
            Q.append([nx, ny, x, y])

ans = min(dist[(H - 1, W - 1, H - 2, W - 1)], dist[(H - 1, W - 1, H - 1, W - 2)])
print(ans) if ans != inf else print(-1)
0