結果

問題 No.2731 Two Colors
ユーザー なえしら
提出日時 2024-08-13 23:49:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 770 ms / 3,000 ms
コード長 652 bytes
コンパイル時間 397 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 264,196 KB
最終ジャッジ日時 2024-08-13 23:49:39
合計ジャッジ時間 12,417 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush

DIJ = [(-1, 0), (0, -1), (0, 1), (1, 0)]

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

IJ = {A[i][j]: (i, j) for i in range(H) for j in range(W)}

color = [[-1]*W for _ in range(H)]
que = [[A[-1][-1]], [A[0][0]]]

for t in range(-1, H*W-1):
  i, j = IJ[heappop(que[t%2])]
  color[i][j] = t%2
  for ni, nj in DIJ:
    ni += i; nj += j
    if not H > ni >= 0 <= nj < W: continue
    if color[ni][nj] == -1 or color[ni][nj] == t%2-3:
      color[ni][nj] -= t%2+1
      heappush(que[t%2], A[ni][nj])
    elif color[ni][nj] == 1-t%2: break
  else: continue

  print(t); break
0