結果

問題 No.2731 Two Colors
ユーザー ntudantuda
提出日時 2024-04-20 10:56:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,246 ms / 3,000 ms
コード長 809 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 121,008 KB
最終ジャッジ日時 2024-04-20 10:57:03
合計ジャッジ時間 16,380 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,244 ms
107,284 KB
testcase_01 AC 1,240 ms
107,576 KB
testcase_02 AC 1,246 ms
107,616 KB
testcase_03 AC 116 ms
76,748 KB
testcase_04 AC 160 ms
78,464 KB
testcase_05 AC 162 ms
78,208 KB
testcase_06 AC 227 ms
84,096 KB
testcase_07 AC 200 ms
81,916 KB
testcase_08 AC 133 ms
77,568 KB
testcase_09 AC 366 ms
99,600 KB
testcase_10 AC 146 ms
76,792 KB
testcase_11 AC 578 ms
117,704 KB
testcase_12 AC 362 ms
99,960 KB
testcase_13 AC 584 ms
111,764 KB
testcase_14 AC 276 ms
87,504 KB
testcase_15 AC 160 ms
78,408 KB
testcase_16 AC 331 ms
91,516 KB
testcase_17 AC 336 ms
94,512 KB
testcase_18 AC 172 ms
79,336 KB
testcase_19 AC 338 ms
92,988 KB
testcase_20 AC 255 ms
89,548 KB
testcase_21 AC 235 ms
80,012 KB
testcase_22 AC 473 ms
107,924 KB
testcase_23 AC 226 ms
84,744 KB
testcase_24 AC 189 ms
80,612 KB
testcase_25 AC 130 ms
76,504 KB
testcase_26 AC 337 ms
97,280 KB
testcase_27 AC 435 ms
105,264 KB
testcase_28 AC 430 ms
100,056 KB
testcase_29 AC 224 ms
83,044 KB
testcase_30 AC 242 ms
86,784 KB
testcase_31 AC 243 ms
86,316 KB
testcase_32 AC 631 ms
121,008 KB
testcase_33 AC 41 ms
52,736 KB
testcase_34 AC 42 ms
52,608 KB
testcase_35 AC 42 ms
52,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import *

H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
M = [[-1] * W for _ in range(H)]
M[0][0] = 0
M[H - 1][W - 1] = 1
dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]
q0 = [(A[0][1], (0, 1)), (A[1][0], (1, 0))]
heapify(q0)
q1 = [(A[H - 1][W - 2], (H - 1, W - 2)), (A[H - 2][W - 1], (H - 2, W - 1))]
heapify(q1)
Q = [q0, q1]

i = 0
while 1:
    i2 = i % 2
    a, (x, y) = heappop(Q[i2])
    if M[x][y] == -1:
        M[x][y] = i2
        for xd, yd in dir:
            x2 = x + xd
            y2 = y + yd
            if 0 <= x2 < H and 0 <= y2 < W:
                if M[x2][y2] == i2 ^ 1:
                    print(i + 1)
                    exit()
                elif M[x2][y2] == -1:
                    heappush(Q[i2], (A[x2][y2], (x2, y2)))
        i += 1
0