← 返回 amazon 的题目列表Minimize Grid Inconvenience by Adding One Delivery Center
类型:online_judge
You are given an n × m binary grid grid:
grid[r][c] = 1 means the cell contains a delivery center.
grid[r][c] = 0 means the cell is empty.
The distance between cells (r1, c1) and (r2, c2) is the Chebyshev distance:
max(|r1 - r2|, |c1 - c2|)
The distance of an empty cell is its distance to the nearest delivery center. The grid's inconvenience is the maximum such distance over all empty cells.
You may convert at most one 0 cell into a 1 cell, thereby adding one delivery center. Return the minimum possible inconvenience after the optional conversion.
Input Format
Standard input contains a JSON 2D integer array, for example:
[[0,0,0,1],[0,0,0,1]]
Output Format
Print one integer: the minimum possible inconvenience.
Example 1
Input:
[[0,0,0,1],
[0,0,0,1]]
Output:
1
Explanation: Adding a delivery center at (0, 0) makes every empty cell at distance at most 1 from a center.
Example 2
Input:
[[1,0,0],
[0,0,0],
[0,0,0]]
Output:
1
Constraints
1 <= n, m <= 500
grid[r][c] is either 0 or 1.
There may be no initial delivery centers.
Example
Input
[[0,0,0,1],[0,0,0,1]]
Output
1