結果

問題 No.2731 Two Colors
ユーザー なえしら
提出日時 2024-08-13 23:45:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,470 ms / 3,000 ms
コード長 620 bytes
コンパイル時間 437 ms
コンパイル使用メモリ 82,264 KB
実行使用メモリ 107,696 KB
最終ジャッジ日時 2024-08-13 23:45:44
合計ジャッジ時間 17,731 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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)]

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

for t in range(-1, H*W-1):
  a, i, j = 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], ni, nj))
    elif color[ni][nj] == 1-t%2: break
  else: continue
  print(t); break
0