結果

問題 No.2731 Two Colors
ユーザー june19312june19312
提出日時 2024-04-20 09:56:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,862 ms / 3,000 ms
コード長 1,400 bytes
コンパイル時間 1,099 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 130,944 KB
最終ジャッジ日時 2024-04-20 09:57:11
合計ジャッジ時間 26,243 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,844 ms
118,808 KB
testcase_01 AC 2,820 ms
118,516 KB
testcase_02 AC 2,862 ms
118,636 KB
testcase_03 AC 121 ms
78,200 KB
testcase_04 AC 162 ms
78,752 KB
testcase_05 AC 245 ms
79,080 KB
testcase_06 AC 283 ms
86,420 KB
testcase_07 AC 234 ms
83,400 KB
testcase_08 AC 143 ms
77,952 KB
testcase_09 AC 508 ms
104,300 KB
testcase_10 AC 134 ms
77,788 KB
testcase_11 AC 1,030 ms
130,432 KB
testcase_12 AC 520 ms
103,524 KB
testcase_13 AC 890 ms
120,160 KB
testcase_14 AC 387 ms
91,368 KB
testcase_15 AC 145 ms
78,328 KB
testcase_16 AC 452 ms
96,028 KB
testcase_17 AC 466 ms
99,388 KB
testcase_18 AC 250 ms
80,808 KB
testcase_19 AC 500 ms
97,140 KB
testcase_20 AC 352 ms
91,880 KB
testcase_21 AC 256 ms
81,108 KB
testcase_22 AC 717 ms
115,332 KB
testcase_23 AC 282 ms
87,556 KB
testcase_24 AC 251 ms
81,268 KB
testcase_25 AC 117 ms
77,120 KB
testcase_26 AC 487 ms
101,244 KB
testcase_27 AC 643 ms
110,696 KB
testcase_28 AC 607 ms
105,640 KB
testcase_29 AC 267 ms
85,456 KB
testcase_30 AC 340 ms
88,968 KB
testcase_31 AC 316 ms
88,304 KB
testcase_32 AC 987 ms
130,944 KB
testcase_33 AC 35 ms
52,224 KB
testcase_34 AC 36 ms
52,736 KB
testcase_35 AC 36 ms
52,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop,heappush
H,W = map(int,input().split())
G = []
color = []

for i in range(H):
    G.append(list(map(int,input().split())))
    color.append([-1]*W)

now1 = (0,0)
now2 = (H-1,W-1)

Q1,Q2 = [],[]
heappush(Q1,(G[0][0],0,0))
heappush(Q2,(G[H-1][W-1],H-1,W-1))

cnt = 0
dir = [(-1,0),(1,0),(0,-1),(0,1)]

while True:
    if cnt % 2 == 0:
        if Q1:
            val,h,w = heappop(Q1)

            if color[h][w] != -1:
                continue

            color[h][w] = 0
            cnt += 1

            for a,b in dir:
                if 0 <= h+a < H and 0 <= w+b < W:
                    if color[h+a][w+b] == 1:
                        print(cnt-2)
                        exit()
                    elif color[h+a][w+b] == -1:
                        heappush(Q1,(G[h+a][w+b],h+a,w+b))
        else:
            print(cnt-2)
            exit()
    else:
        if Q2:
            val,h,w = heappop(Q2)
            if color[h][w] != -1:
                continue
            color[h][w] = 1
            cnt += 1
            for a,b in dir:
                if 0 <= h+a < H and 0 <= w+b < W:
                    if color[h+a][w+b] == 0:
                        print(cnt-2)
                        exit()
                    elif color[h+a][w+b] == -1:
                        heappush(Q2,(G[h+a][w+b],h+a,w+b))
        else:
            print(cnt-2)
            exit()


0