結果

問題 No.2731 Two Colors
ユーザー PNJ
提出日時 2024-04-19 23:26:34
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 873 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 65,536 KB
最終ジャッジ日時 2024-10-11 18:04:21
合計ジャッジ時間 4,890 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from sortedcontainers import SortedList
H,W = map(int,input().split())
A = [list(map(int,input().split())) for i in range(H)]
P = SortedList()
Q = SortedList()
C = [[-1 for j in range(W+1)] for i in range(H+1)]
C[0][0] = 1
C[H-1][W-1] = 0
P.add((-A[0][1],0,1))
P.add((-A[1][0],1,0))
Q.add((-A[H-1][W-2],H-1,W-2))
Q.add((-A[H-2][W-1],H-2,W-1))
D = [(1,0),(-1,0),(0,-1),(0,1)]
ans = 0
for _ in range(H*W):
  ans += 1
  if ans % 2:
    a,i,j = P.pop()
    while C[i][j] != -1:
      a,i,j = P.pop()
  else:
    a,i,j = Q.pop()
    while C[i][j] != -1:
      a,i,j = Q.pop()
  C[i][j] = ans % 2
  for dx,dy in D:
    if C[i+dx][j+dy] == (ans + 1) % 2:
      print(ans)
      exit()
    if C[i+dx][j+dy] == -1:
      if 0 <= i + dx < H and 0 <= j + dy < W:
        if ans % 2:
          P.add((-A[i+dx][j+dy],i+dx,j+dy))
        else:
          Q.add((-A[i+dx][j+dy],i+dx,j+dy))
0