結果

問題 No.2731 Two Colors
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2024-04-19 23:25:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 850 ms / 3,000 ms
コード長 1,603 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 82,344 KB
実行使用メモリ 258,232 KB
最終ジャッジ日時 2024-04-19 23:25:28
合計ジャッジ時間 16,159 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 829 ms
257,952 KB
testcase_01 AC 850 ms
257,796 KB
testcase_02 AC 828 ms
258,232 KB
testcase_03 AC 127 ms
78,184 KB
testcase_04 AC 169 ms
85,728 KB
testcase_05 AC 146 ms
82,052 KB
testcase_06 AC 240 ms
110,160 KB
testcase_07 AC 260 ms
124,248 KB
testcase_08 AC 139 ms
80,460 KB
testcase_09 AC 616 ms
188,232 KB
testcase_10 AC 129 ms
78,164 KB
testcase_11 AC 612 ms
175,280 KB
testcase_12 AC 628 ms
189,480 KB
testcase_13 AC 551 ms
192,932 KB
testcase_14 AC 351 ms
140,000 KB
testcase_15 AC 136 ms
79,108 KB
testcase_16 AC 325 ms
137,364 KB
testcase_17 AC 407 ms
165,292 KB
testcase_18 AC 164 ms
86,300 KB
testcase_19 AC 341 ms
137,884 KB
testcase_20 AC 456 ms
160,356 KB
testcase_21 AC 195 ms
88,704 KB
testcase_22 AC 650 ms
239,824 KB
testcase_23 AC 261 ms
109,312 KB
testcase_24 AC 193 ms
94,748 KB
testcase_25 AC 110 ms
77,536 KB
testcase_26 AC 545 ms
219,796 KB
testcase_27 AC 625 ms
209,480 KB
testcase_28 AC 441 ms
158,264 KB
testcase_29 AC 226 ms
105,116 KB
testcase_30 AC 322 ms
135,420 KB
testcase_31 AC 262 ms
107,508 KB
testcase_32 AC 762 ms
225,252 KB
testcase_33 AC 37 ms
54,308 KB
testcase_34 AC 36 ms
53,228 KB
testcase_35 AC 35 ms
54,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
H,W = map(int,input().split())

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

dic = {}
lis = []
for i in range(H):
    for j in range(W):
        lis.append( A[i][j] )
lis.sort()
for i in range(len(lis)):
    dic[lis[i]] = i
for i in range(H):
    for j in range(W):
        A[i][j] = dic[A[i][j]]

rev = [0] * (H*W)
for i in range(H):
    for j in range(W):
        rev[A[i][j]] = (i,j)

color = [["#"] * W for i in range(H)]
state = [[0] * W for i in range(H)]

q0 = []
q1 = []

color[0][0] = 1
q1.append( A[0][0] )
color[H-1][W-1] = 0
q0.append( A[H-1][W-1] )


ans = -2

for i in range(H*W+1):

    flag = False
    if i % 2 == 1:

        num = heapq.heappop(q0)
        x,y = rev[num]
        color[x][y] = 0

        for xx,yy in ( (x-1,y),(x+1,y),(x,y-1),(x,y+1) ):
            if not (0 <= xx < H and 0 <= yy < W):
                continue
            if state[xx][yy] & 1 == 0 and color[xx][yy] == "#":
                state[xx][yy] |= 1
                heapq.heappush( q0 , A[xx][yy])

            if color[xx][yy] == 1:
                flag = True

    else:

        num = heapq.heappop(q1)
        x,y = rev[num]
        color[x][y] = 1

        for xx,yy in ( (x-1,y),(x+1,y),(x,y-1),(x,y+1) ):
            if not (0 <= xx < H and 0 <= yy < W):
                continue
            if state[xx][yy] & 2 == 0 and color[xx][yy] == "#":
                state[xx][yy] |= 2
                heapq.heappush( q1 , A[xx][yy])

            if color[xx][yy] == 0:
                flag = True

    ans += 1
    #print (color)
    if flag:
        break

print (ans)
        
0