作者:
JIWP (JIWP)
2025-01-22 23:21:391765. Map of Highest Peak
思路:
先記錄一下水在哪幾格
並且用level紀錄現在的高度
接著就bfs就好
golang code :
func highestPeak(isWater [][]int) [][]int {
visited := [1001][1001]bool{}
queue := []int{}
n, m := len(isWater), len(isWater[0])
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if isWater[i][j] == 1 {
visited[i][j] = true
queue = append(queue, i*m+j)
isWater[i][j] = 0
}
}
}
level := 1
chk := func(x, y int) {
if x > -1 && y > -1 && x < n && y < m && !visited[x][y] {
isWater[x][y] = level
visited[x][y] = true
queue = append(queue, x*m+y)
}
}
for len(queue) > 0 {
cnt := len(queue)
for cnt > 0 {
cur_node := queue[0]
x, y := cur_node/m, cur_node%m
queue = queue[1:]
cnt