結果

問題 No.2731 Two Colors
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2024-04-19 23:19:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,536 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 82,040 KB
実行使用メモリ 257,860 KB
最終ジャッジ日時 2024-04-19 23:20:01
合計ジャッジ時間 17,562 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 868 ms
257,696 KB
testcase_01 AC 928 ms
257,720 KB
testcase_02 AC 931 ms
257,860 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 166 ms
81,664 KB
testcase_06 AC 274 ms
110,336 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 720 ms
185,220 KB
testcase_13 AC 619 ms
192,896 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 183 ms
85,632 KB
testcase_19 AC 367 ms
137,984 KB
testcase_20 AC 493 ms
160,128 KB
testcase_21 AC 202 ms
88,320 KB
testcase_22 AC 704 ms
239,488 KB
testcase_23 AC 285 ms
109,184 KB
testcase_24 WA -
testcase_25 AC 123 ms
77,384 KB
testcase_26 AC 586 ms
219,612 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 237 ms
104,320 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 41 ms
52,480 KB
testcase_34 WA -
testcase_35 WA -
権限があれば一括ダウンロードができます

ソースコード

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 = [[-1] * 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 = 0

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

    flag = False
    if i % 2 == 0:

        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:
                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:
                state[xx][yy] |= 2
                heapq.heappush( q1 , A[xx][yy])

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

    ans += 1
    if flag:
        break

print (ans - 3)
        
0