結果

問題 No.2731 Two Colors
ユーザー i_takui_taku
提出日時 2024-04-20 16:01:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,666 ms / 3,000 ms
コード長 1,578 bytes
コンパイル時間 409 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 120,048 KB
最終ジャッジ日時 2024-04-20 16:01:27
合計ジャッジ時間 21,698 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,644 ms
119,988 KB
testcase_01 AC 1,639 ms
119,924 KB
testcase_02 AC 1,666 ms
120,048 KB
testcase_03 AC 158 ms
77,512 KB
testcase_04 AC 195 ms
79,204 KB
testcase_05 AC 188 ms
78,728 KB
testcase_06 AC 296 ms
83,236 KB
testcase_07 AC 278 ms
83,892 KB
testcase_08 AC 170 ms
78,464 KB
testcase_09 AC 508 ms
105,112 KB
testcase_10 AC 158 ms
78,452 KB
testcase_11 AC 938 ms
111,792 KB
testcase_12 AC 496 ms
103,740 KB
testcase_13 AC 792 ms
105,912 KB
testcase_14 AC 334 ms
89,280 KB
testcase_15 AC 187 ms
77,952 KB
testcase_16 AC 462 ms
89,836 KB
testcase_17 AC 470 ms
94,196 KB
testcase_18 AC 230 ms
80,372 KB
testcase_19 AC 495 ms
90,904 KB
testcase_20 AC 341 ms
94,048 KB
testcase_21 AC 239 ms
80,516 KB
testcase_22 AC 837 ms
108,580 KB
testcase_23 AC 327 ms
85,016 KB
testcase_24 AC 253 ms
80,440 KB
testcase_25 AC 140 ms
77,732 KB
testcase_26 AC 498 ms
99,732 KB
testcase_27 AC 626 ms
107,280 KB
testcase_28 AC 561 ms
98,112 KB
testcase_29 AC 278 ms
82,840 KB
testcase_30 AC 371 ms
87,264 KB
testcase_31 AC 321 ms
84,424 KB
testcase_32 AC 942 ms
118,276 KB
testcase_33 AC 45 ms
52,864 KB
testcase_34 AC 44 ms
52,864 KB
testcase_35 AC 44 ms
52,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush


H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
di, dj = [1, 0, -1, 0], [0, 1, 0, -1]
hq1 = []
hq2 = []
C = [[-1] * W for _ in range(H)]
C[0][0] = 0
C[-1][-1] = 1
add1 = [[False] * W for _ in range(H)]
add2 = [[False] * W for _ in range(H)]
for d in range(4):
    ni, nj = di[d], dj[d]
    if 0 <= ni < H and 0 <= nj < W:
        add1[ni][nj] = True
        heappush(hq1, (A[ni][nj], ni, nj))
for d in range(4):
    ni, nj = H - 1 + di[d], W - 1 + dj[d]
    if 0 <= ni < H and 0 <= nj < W:
        add2[ni][nj] = True
        heappush(hq2, (A[ni][nj], ni, nj))

ans = 1
while hq1 and hq2:
    if ans & 1:
        v, i, j = heappop(hq1)
        C[i][j] = 0
        for d in range(4):
            ni, nj = i + di[d], j + dj[d]
            if 0 <= ni < H and 0 <= nj < W and C[ni][nj] != -1 and C[ni][nj] != C[i][j]:
                print(ans)
                exit()
            if 0 <= ni < H and 0 <= nj < W and C[ni][nj] == -1 and not add1[ni][nj]:
                add1[ni][nj] = True
                heappush(hq1, (A[ni][nj], ni, nj))
    else:
        v, i, j = heappop(hq2)
        C[i][j] = 1
        for d in range(4):
            ni, nj = i + di[d], j + dj[d]
            if 0 <= ni < H and 0 <= nj < W and C[ni][nj] != -1 and C[ni][nj] != C[i][j]:
                print(ans)
                exit()
            if 0 <= ni < H and 0 <= nj < W and C[ni][nj] == -1 and not add2[ni][nj]:
                add2[ni][nj] = True
                heappush(hq2, (A[ni][nj], ni, nj))
    ans += 1
assert 0
0