Give you an integer matrix isWater with the size of m x n, which represents a map composed of land and water cells.
If isWater[i][j] == 0, the grid (i, j) is a land grid.
If isWater[i][j] == 1, grid (i, j) is a water grid.
You need to arrange the height of each cell according to the following rules:
The height of each lattice must be nonnegative.
If a grid is a water area, its height must be 0.
The height difference of any adjacent grid is at most 1. When two grids are close to each other in due east, South, West and North, they are called adjacent grids. (that is, they have a common edge)
Find a scheme to arrange the height so that the highest height value in the matrix is the largest.
Please return an integer matrix height of size m x n, where height[i][j] is the height of lattice (i, j). If there are multiple solutions, please return any one.
Example 1:
Input: isWater = [[0,1],[0,0]]
Output: [[1,0], [2,1]]
Explanation: the figure above shows the height of each grid.
The blue grid is the water grid and the green grid is the land grid.
Example 2:
Input: isWater = [[0,0,1],[1,0,0],[0,0,0]]
Output: [[1,1,0], [0,1,1], [1,2,2]]
Explanation: among all arrangements, the maximum feasible height is 2.
In any arrangement scheme, as long as the maximum height is 2 and meets the above rules, it is a feasible scheme.
Tips:
m == isWater.length
n == isWater[i].length
1 <= m, n <= 1000
isWater[i][j] is either 0 or 1.
At least 1 water grid.
Source: LeetCode
Link: https://leetcode-cn.com/problems/map-of-highest-peak
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.
Bare BFS can be performed directly with each water source as the origin. Because the height is required to be as large as possible, the height of the four positions around each position accessed each time is directly set to the current position + 1 (if it has not been accessed). It is easy to prove that there will be no cliff in this way (analogy to contour line). At the beginning, the vis array in the code was not judged, resulting in several rounds of wa==
class Solution { public: #define fi first #define se second int a[1005][1005], n, m; queue<pair<pair<int, int>, int> > q; int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; bool vis[1005][1005]; void bfs() { while(q.size()) { pair<pair<int, int>, int> now = q.front(); q.pop(); if(vis[now.fi.fi][now.fi.se]) continue; a[now.fi.fi][now.fi.se] = now.se; vis[now.fi.fi][now.fi.se] = 1; for(int i = 0; i < 4; i++) { int nx = now.fi.fi + dir[i][0], ny = now.fi.se + dir[i][1]; if(nx > n || nx < 1 || ny > m || ny < 1 || vis[nx][ny]) continue; q.push(make_pair(make_pair(nx, ny), now.se + 1)); } } } vector<vector<int>> highestPeak(vector<vector<int>>& isWater) { memset(vis, 0, sizeof(vis)); n = isWater.size(), m = isWater[0].size(); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { a[i + 1][j + 1] = -isWater[i][j]; if(a[i + 1][j + 1] == -1) { q.push(make_pair(make_pair(i + 1, j + 1), 0)); } else a[i + 1][j + 1] = 0; } } bfs(); vector<vector<int> > ans(n); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(a[i + 1][j + 1] != -1) ans[i].push_back(a[i + 1][j + 1]); else ans[i].push_back(0); } } return ans; } };