結果

問題 No.2731 Two Colors
ユーザー rlangevinrlangevin
提出日時 2024-04-19 22:06:44
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,629 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 311,596 KB
最終ジャッジ日時 2024-04-19 22:07:52
合計ジャッジ時間 19,363 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,769 ms
311,596 KB
testcase_01 AC 2,826 ms
131,444 KB
testcase_02 AC 2,767 ms
131,212 KB
testcase_03 AC 145 ms
77,656 KB
testcase_04 AC 341 ms
82,816 KB
testcase_05 AC 239 ms
80,072 KB
testcase_06 AC 784 ms
94,208 KB
testcase_07 AC 1,042 ms
102,300 KB
testcase_08 AC 198 ms
79,432 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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 = [(0, sx, sy)]
    now = 0
    seen = [[0] * W for _ in range(H)]
    seen[sx][sy] = 1
    while HH:
        _, px, py = 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, (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