結果

問題 No.2855 Move on Grid
ユーザー Basin-Bug
提出日時 2024-08-25 14:54:58
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 964 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 82,476 KB
実行使用メモリ 125,804 KB
最終ジャッジ日時 2024-08-25 14:55:49
合計ジャッジ時間 49,741 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 TLE * 11 -- * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
N, M, K = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]

def slove(grid, N, M, K, X):
  changeCount = [[10 ** 18] * M for _ in range(N)]
  changeCount[0][0] = 0 if grid[0][0] >= X else 1
  pq = [(changeCount[0][0], 0, 0)]
  
  directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

  while pq:
    currentChange, i, j = heapq.heappop(pq)
    if i == N - 1 and j == M - 1:
      return currentChange <= K
    
    for di, dj in directions:
      ni, nj = i + di, j + dj
      if 0 <= ni < N and 0 <= nj < M:
        nextChange = currentChange
        if grid[ni][nj] < X:
          nextChange += 1
        
        if nextChange < changeCount[ni][nj]:
          changeCount[ni][nj] = nextChange
          heapq.heappush(pq, (nextChange, ni, nj))
  return False


low, high = 1, 10 ** 9 + 1

while low < high:
  mid = (low + high) // 2
  if slove(A, N, M, K, mid):
    low = mid + 1
  else:
    high = mid

print(low - 1)

0