LeetCode 1765. Map of Highest Peak
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 most?
1
. 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.
-----------------------------------------------------------
給你一個(gè)大小為 m x n 的整數(shù)矩陣 isWater ,它代表了一個(gè)由 陸地 和 水域 單元格組成的地圖。
如果 isWater[i][j] == 0 ,格子 (i, j) 是一個(gè) 陸地 格子。
如果 isWater[i][j] == 1 ,格子 (i, j) 是一個(gè) 水域 格子。
你需要按照如下規(guī)則給每個(gè)單元格安排高度:
每個(gè)格子的高度都必須是非負(fù)的。
如果一個(gè)格子是 水域 ,那么它的高度必須為 0 。
任意相鄰的格子高度差 至多 為 1 。當(dāng)兩個(gè)格子在正東、南、西、北方向上相互緊挨著,就稱它們?yōu)橄噜彽母褡印#ㄒ簿褪钦f(shuō)它們有一條公共邊)
找到一種安排高度的方案,使得矩陣中的最高高度值 最大 。
請(qǐng)你返回一個(gè)大小為 m x n 的整數(shù)矩陣 height ,其中 height[i][j] 是格子 (i, j) 的高度。如果有多種解法,請(qǐng)返回 任意一個(gè) 。
來(lái)源:力扣(LeetCode)
鏈接:https://leetcode.cn/problems/map-of-highest-peak
經(jīng)典的BFS,只是對(duì)于多源的處理,先將所有的水的地址放到Deque中,然后依次遍歷,
遍歷后,將新的元素放到Deque中繼續(xù)遍歷,(只遍歷標(biāo)記是陸地,且位置高度是-1的,也就是之前沒(méi)有遍歷到的)
下面是代碼:
Runtime:?66 ms, faster than?59.02%?of?Java?online submissions for?Map of Highest Peak.
Memory Usage:?166.9 MB, less than?16.39%?of?Java?online submissions for?Map of Highest Peak.