結果
問題 | No.2731 Two Colors |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:57:28 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,227 bytes |
コンパイル時間 | 329 ms |
コンパイル使用メモリ | 82,136 KB |
実行使用メモリ | 171,980 KB |
最終ジャッジ日時 | 2025-03-26 15:58:48 |
合計ジャッジ時間 | 16,444 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 TLE * 3 |
ソースコード
import heapqdef main():import sysinput = sys.stdin.read().split()idx = 0H = int(input[idx])idx += 1W = int(input[idx])idx += 1A = []for _ in range(H):row = list(map(int, input[idx:idx+W]))idx += WA.append(row)# Using 0-based indicescolor = [[-1 for _ in range(W)] for _ in range(H)]color[0][0] = 1 # (1,1) becomes (0,0)color[H-1][W-1] = 0 # (H,W) becomes (H-1, W-1)heap0 = []heap1 = []dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]# Initialize heap1 with cells adjacent to (0,0)for di, dj in dirs:ni = 0 + dinj = 0 + djif 0 <= ni < H and 0 <= nj < W:if color[ni][nj] == -1:heapq.heappush(heap1, (A[ni][nj], ni, nj))# Initialize heap0 with cells adjacent to (H-1, W-1)for di, dj in dirs:ni = (H-1) + dinj = (W-1) + djif 0 <= ni < H and 0 <= nj < W:if color[ni][nj] == -1:heapq.heappush(heap0, (A[ni][nj], ni, nj))steps = 0while True:steps += 1current_color = steps % 2current_heap = heap1 if current_color == 1 else heap0# Find the next valid cell in the current heapwhile True:if not current_heap:break # This should not happen per problem statementval, i, j = current_heap[0]if color[i][j] == -1:breakheapq.heappop(current_heap)else:break # No more cells to process# Color the cellval, i, j = heapq.heappop(current_heap)color[i][j] = current_color# Check all adjacent cellsfor di, dj in dirs:ni = i + dinj = j + djif 0 <= ni < H and 0 <= nj < W:if color[ni][nj] == 1 - current_color:print(steps)returnelif color[ni][nj] == -1:heapq.heappush(current_heap, (A[ni][nj], ni, nj))print(steps) # This line is theoretically unreachableif __name__ == "__main__":main()