結果

問題 No.2731 Two Colors
ユーザー prd_xxxprd_xxx
提出日時 2024-04-19 21:52:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 720 ms / 3,000 ms
コード長 1,793 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 111,876 KB
最終ジャッジ日時 2024-04-19 21:52:41
合計ジャッジ時間 11,718 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 718 ms
111,488 KB
testcase_01 AC 686 ms
111,500 KB
testcase_02 AC 720 ms
111,876 KB
testcase_03 AC 123 ms
77,160 KB
testcase_04 AC 152 ms
78,560 KB
testcase_05 AC 154 ms
78,700 KB
testcase_06 AC 187 ms
81,432 KB
testcase_07 AC 189 ms
82,840 KB
testcase_08 AC 137 ms
77,764 KB
testcase_09 AC 295 ms
99,804 KB
testcase_10 AC 130 ms
77,932 KB
testcase_11 AC 403 ms
101,768 KB
testcase_12 AC 289 ms
99,860 KB
testcase_13 AC 368 ms
98,060 KB
testcase_14 AC 209 ms
86,792 KB
testcase_15 AC 149 ms
77,332 KB
testcase_16 AC 232 ms
86,036 KB
testcase_17 AC 250 ms
90,376 KB
testcase_18 AC 154 ms
78,488 KB
testcase_19 AC 242 ms
86,488 KB
testcase_20 AC 228 ms
92,060 KB
testcase_21 AC 171 ms
79,812 KB
testcase_22 AC 342 ms
101,868 KB
testcase_23 AC 187 ms
82,040 KB
testcase_24 AC 156 ms
78,928 KB
testcase_25 AC 112 ms
76,512 KB
testcase_26 AC 279 ms
96,072 KB
testcase_27 AC 310 ms
101,040 KB
testcase_28 AC 281 ms
92,484 KB
testcase_29 AC 173 ms
81,080 KB
testcase_30 AC 209 ms
85,252 KB
testcase_31 AC 186 ms
82,168 KB
testcase_32 AC 402 ms
108,840 KB
testcase_33 AC 37 ms
52,864 KB
testcase_34 AC 38 ms
52,864 KB
testcase_35 AC 36 ms
52,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

H,W = map(int,input().split())
A = [list(map(int,input().split())) for i in range(H)]

dij = [(0,1),(1,0),(0,-1),(-1,0)]
import heapq

C = [[-1]*W for _ in range(H)]
C[0][0] = 1
C[H-1][W-1] = 0

hq1 = []
hq0 = []
for di,dj in dij:
    ni = 0 + di
    nj = 0 + dj
    if 0 <= ni < H and 0 <= nj < W:
        heapq.heappush(hq1, (A[ni][nj],ni*W+nj))
for di,dj in dij:
    ni = H-1 + di
    nj = W-1 + dj
    if 0 <= ni < H and 0 <= nj < W:
        heapq.heappush(hq0, (A[ni][nj],ni*W+nj))

added0 = [[0]*W for _ in range(H)]
added1 = [[0]*W for _ in range(H)]
for k in range(H*W+1):
    if k%2==0:
        while hq1:
            a,v = heapq.heappop(hq1)
            i,j = divmod(v,W)
            if C[i][j] != -1:
                continue
            C[i][j] = 1
            for di,dj in dij:
                ni = i + di
                nj = j + dj
                if 0 <= ni < H and 0 <= nj < W:
                    if C[ni][nj] == 0:
                        print(k+1)
                        exit()
                    if C[ni][nj] == -1 and added1[ni][nj] == 0:
                        heapq.heappush(hq1, (A[ni][nj],ni*W+nj))
                        added1[ni][nj] = 1
            break
    else:
        while hq0:
            a,v = heapq.heappop(hq0)
            i,j = divmod(v,W)
            if C[i][j] != -1:
                continue
            C[i][j] = 0
            for di,dj in dij:
                ni = i + di
                nj = j + dj
                if 0 <= ni < H and 0 <= nj < W:
                    if C[ni][nj] == 1:
                        print(k+1)
                        exit()
                    if C[ni][nj] == -1 and added0[ni][nj] == 0:
                        heapq.heappush(hq0, (A[ni][nj],ni*W+nj))
                        added0[ni][nj] = 1
            break
0