結果

問題 No.2731 Two Colors
ユーザー rlangevinrlangevin
提出日時 2024-04-19 22:10:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,134 ms / 3,000 ms
コード長 1,763 bytes
コンパイル時間 142 ms
コンパイル使用メモリ 82,256 KB
実行使用メモリ 138,888 KB
最終ジャッジ日時 2024-04-19 22:10:58
合計ジャッジ時間 21,459 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,007 ms
118,856 KB
testcase_01 AC 1,061 ms
118,832 KB
testcase_02 AC 1,034 ms
118,956 KB
testcase_03 AC 143 ms
78,096 KB
testcase_04 AC 208 ms
79,676 KB
testcase_05 AC 193 ms
79,104 KB
testcase_06 AC 322 ms
85,120 KB
testcase_07 AC 381 ms
88,544 KB
testcase_08 AC 158 ms
78,132 KB
testcase_09 AC 1,134 ms
132,768 KB
testcase_10 AC 133 ms
77,572 KB
testcase_11 AC 886 ms
118,544 KB
testcase_12 AC 1,092 ms
131,636 KB
testcase_13 AC 772 ms
110,132 KB
testcase_14 AC 537 ms
99,564 KB
testcase_15 AC 159 ms
78,240 KB
testcase_16 AC 410 ms
91,628 KB
testcase_17 AC 625 ms
102,176 KB
testcase_18 AC 186 ms
79,836 KB
testcase_19 AC 430 ms
92,624 KB
testcase_20 AC 788 ms
113,684 KB
testcase_21 AC 234 ms
81,176 KB
testcase_22 AC 1,117 ms
130,996 KB
testcase_23 AC 354 ms
86,984 KB
testcase_24 AC 239 ms
80,988 KB
testcase_25 AC 146 ms
77,884 KB
testcase_26 AC 895 ms
119,344 KB
testcase_27 AC 1,081 ms
131,292 KB
testcase_28 AC 620 ms
104,456 KB
testcase_29 AC 270 ms
83,408 KB
testcase_30 AC 455 ms
93,664 KB
testcase_31 AC 311 ms
84,464 KB
testcase_32 AC 1,129 ms
138,888 KB
testcase_33 AC 36 ms
52,864 KB
testcase_34 AC 37 ms
53,248 KB
testcase_35 AC 38 ms
53,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

M = 1005
def f(c, x, y):
    return y + x * M + c * M * M
def g(v):
    y = v % M
    c, x = divmod(v//M, M)
    return c, x, y

H, W = map(int, input().split())
A = []
for i in range(H):
    A.append(list(map(int, input().split())))
    
from heapq import *
def bfs(sx, sy, H, W):
    dist = [[-1] * W for _ in range(H)]
    dist[sx][sy] = 0
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    HH = [f(0, sx, sy)]
    now = 0
    seen = [[0] * W for _ in range(H)]
    seen[sx][sy] = 1
    while HH:
        _, px, py = g(heappop(HH))
        dist[px][py] = now
        now += 1
        for k in range(4):
            x = px + dx[k]
            y = py + dy[k]
            if x < 0 or x > H - 1 or y < 0 or y > W - 1:
                continue
            if seen[x][y]:
                continue
            seen[x][y] = 1
            heappush(HH, f(A[x][y], x, y))
            
    return dist

d1 = bfs(0, 0, H, W)
d2 = bfs(H - 1, W - 1, H, W)

ans = 10 ** 18
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
for i in range(H):
    for j in range(W):
        now = d1[i][j]
        flag = 0
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x > H - 1 or y < 0 or y > W - 1:
                continue
            if d2[x][y] < now:
                flag = 1
                break
        if flag:
            ans = min(ans, 2 * now - 1)
            
        now = d2[i][j]
        flag = 0
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x > H - 1 or y < 0 or y > W - 1:
                continue
            if d1[x][y] <= now:
                flag = 1
                break
        if flag:
            ans = min(ans, 2 * now)
            
                  
print(ans)
0