You are given an integer matrix isWater of size m x n that represents a map of land and water cells.
If isWater[i][j] == 0, cell (i, j) is a land cell.
If isWater[i][j] == 1, cell (i, j) is a water cell.
You must assign each cell a height in a way that follows these rules:
The height of each cell must be non-negative.
If the cell is a water cell, its height must be 0.
Any two adjacent cells must have an absolute height difference of at most1. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).
Find an assignment of heights such that the maximum height in the matrix is maximized.
Return an integer matrix height of size m x n where height[i][j] is cell (i, j)'s height. If there are multiple solutions, return any of them.
Example 1:
Input: isWater = [[0,1],[0,0]]
Output: [[1,0],[2,1]]
Explanation: The image shows the assigned heights of each cell.
The blue cell is the water cell, and the green cells are the land cells.
Example 2:
Input: isWater = [[0,0,1],[1,0,0],[0,0,0]]
Output: [[1,1,0],[0,1,1],[1,2,2]]
Explanation: A height of 2 is the maximum possible height of any assignment.
Any height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.
usestd::collections::VecDeque;implSolution{#[allow(dead_code)]pubfnhighest_peak(is_water:Vec<Vec<i32>>)->Vec<Vec<i32>>{letn=is_water.len();letm=is_water[0].len();letmutret_vec=vec![vec![-1;m];n];letmutq:VecDeque<(usize,usize)>=VecDeque::new();letvis_pair:Vec<(i32,i32)>=vec![(-1,0),(1,0),(0,-1),(0,1)];// Initialize the return vectorforiin0..n{forjin0..m{ifis_water[i][j]==1{// This cell is water, the height of which must be 0ret_vec[i][j]=0;q.push_back((i,j));}}}while!q.is_empty(){// Get the front X-Y Coordinateslet(x,y)=q.front().unwrap().clone();q.pop_front();// Traverse through the vis pairfordin&vis_pair{let(dx,dy)=*d;ifSelf::check_bounds((xasi32)+dx,(yasi32)+dy,nasi32,masi32){ifret_vec[((xasi32)+dx)asusize][((yasi32)+dy)asusize]==-1{// This cell hasn't been visited, update its heightret_vec[((xasi32)+dx)asusize][((yasi32)+dy)asusize]=ret_vec[x][y]+1;// Enqueue the current cellq.push_back((((xasi32)+dx)asusize,((yasi32)+dy)asusize));}}}}ret_vec}#[allow(dead_code)]fncheck_bounds(i:i32,j:i32,n:i32,m:i32)->bool{i>=0&&i<n&&j>=0&&j<m}}