結果

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

ソースコード

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):
  while True:
    a, i, j = heappop(que[t%2])
    if color[i][j] == -1: break
  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:
      heappush(que[t%2], (A[ni][nj], ni, nj))
    elif color[ni][nj] != t%2: break
  else: continue
  print(t); break
0