結果

問題 No.2731 Two Colors
ユーザー hotate29hotate29
提出日時 2024-04-19 22:05:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,475 ms / 3,000 ms
コード長 1,170 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 82,424 KB
実行使用メモリ 119,408 KB
最終ジャッジ日時 2024-04-19 22:05:35
合計ジャッジ時間 17,713 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,474 ms
119,396 KB
testcase_01 AC 1,445 ms
119,292 KB
testcase_02 AC 1,475 ms
119,408 KB
testcase_03 AC 112 ms
77,420 KB
testcase_04 AC 143 ms
78,844 KB
testcase_05 AC 145 ms
78,592 KB
testcase_06 AC 231 ms
83,944 KB
testcase_07 AC 216 ms
83,308 KB
testcase_08 AC 116 ms
78,296 KB
testcase_09 AC 441 ms
104,040 KB
testcase_10 AC 115 ms
77,592 KB
testcase_11 AC 815 ms
111,432 KB
testcase_12 AC 422 ms
103,992 KB
testcase_13 AC 684 ms
105,976 KB
testcase_14 AC 277 ms
88,676 KB
testcase_15 AC 130 ms
77,944 KB
testcase_16 AC 351 ms
89,696 KB
testcase_17 AC 389 ms
94,080 KB
testcase_18 AC 167 ms
79,488 KB
testcase_19 AC 385 ms
90,576 KB
testcase_20 AC 305 ms
94,028 KB
testcase_21 AC 182 ms
80,168 KB
testcase_22 AC 584 ms
107,856 KB
testcase_23 AC 240 ms
83,724 KB
testcase_24 AC 167 ms
80,332 KB
testcase_25 AC 93 ms
76,800 KB
testcase_26 AC 434 ms
100,192 KB
testcase_27 AC 497 ms
107,216 KB
testcase_28 AC 458 ms
97,356 KB
testcase_29 AC 217 ms
82,780 KB
testcase_30 AC 259 ms
87,272 KB
testcase_31 AC 287 ms
84,484 KB
testcase_32 AC 781 ms
118,848 KB
testcase_33 AC 36 ms
54,984 KB
testcase_34 AC 36 ms
53,604 KB
testcase_35 AC 37 ms
53,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush

h, w = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]

# くっつくまでやる
a_q = [(a[0][0], 0, 0)]
pushed_a = [[False] * w for _ in range(h)]

b_q = [(a[-1][-1], h - 1, w - 1)]
pushed_b = [[False] * w for _ in range(h)]

colors = [[-1] * w for _ in range(h)]

go = True
turn = 0
cnt = 0
DXDY = [(1, 0), (0, 1), (-1, 0), (0, -1)]

while go:
    # print(a_q, b_q, turn)

    if turn == 0:
        _, y, x = heappop(a_q)
    else:
        _, y, x = heappop(b_q)

    if colors[y][x] != -1:
        continue

    colors[y][x] = turn
    for dx, dy in DXDY:
        nx, ny = x + dx, y + dy
        if 0 <= nx < w and 0 <= ny < h:
            if colors[ny][nx] == -1:
                if turn == 0 and not pushed_a[ny][nx]:
                    heappush(a_q, (a[ny][nx], ny, nx))
                    pushed_a[ny][nx] = True
                elif turn == 1 and not pushed_b[ny][nx]:
                    heappush(b_q, (a[ny][nx], ny, nx))
                    pushed_b[ny][nx] = True

            elif colors[ny][nx] != turn:
                go = False

    turn = 1 - turn
    cnt += 1

print(cnt - 2)
0