結果

問題 No.2731 Two Colors
ユーザー ThetaTheta
提出日時 2024-04-23 19:37:32
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,147 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 243,496 KB
最終ジャッジ日時 2024-04-23 19:37:43
合計ジャッジ時間 9,619 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
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 #

from heapq import heappop, heappush
from itertools import count

DIJ = ((0, 1), (1, 0), (0, -1), (-1, 0))


def main():
    H, W = map(int, input().split())
    board = [list(map(int, input().split())) for _ in range(H)]

    color_0 = set(((H - 1, W - 1),))
    color_1 = set(((0, 0),))

    color_0_neighbors = []
    color_1_neighbors = []
    heappush(color_0_neighbors, (board[H - 2][W - 1], H - 2, W - 1))
    heappush(color_0_neighbors, (board[H - 1][W - 2], H - 1, W - 2))
    heappush(color_1_neighbors, (board[1][0], 1, 0))
    heappush(color_1_neighbors, (board[0][1], 0, 1))

    for idx in count(1):

        if idx % 2 == 1:
            h, w = 0, 0
            while (h, w) in color_1:
                num, h, w = heappop(color_1_neighbors)

            color_1.add((h, w))
            for dij in DIJ:
                neighbor = (h + dij[0], w + dij[1])
                if not (0 <= neighbor[0] < H) or not (0 <= neighbor[1] < W):
                    continue
                if neighbor in color_0:
                    break
                if neighbor in color_1:
                    continue
                heappush(color_1_neighbors,
                         (board[neighbor[0]][neighbor[1]],
                          neighbor[0],
                             neighbor[1]))
            else:
                continue
            break
        else:
            h, w = H - 1, W - 1
            while (h, w) in color_0:
                num, h, w = heappop(color_0_neighbors)
            color_0.add((h, w))
            for dij in DIJ:
                neighbor = (h + dij[0], w + dij[1])
                if not (0 <= neighbor[0] < H) or not (0 <= neighbor[1] < W):
                    continue
                if neighbor in color_1:
                    break
                if neighbor in color_0:
                    continue
                heappush(color_0_neighbors,
                         (board[neighbor[0]][neighbor[1]],
                          neighbor[0],
                             neighbor[1]))
            else:
                continue
            break
    print(idx)


if __name__ == "__main__":
    main()
0