Re: [閒聊] 每日leetcode
1765. 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--
chk(x+1, y)
chk(x-1, y)
chk(x, y+1)
chk(x, y-1)
}
level++
}
return isWater
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.72.229.235 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1737559301.A.6D6.html
推
01/22 23:59,
10月前
, 1F
01/22 23:59, 1F
→
01/22 23:59,
10月前
, 2F
01/22 23:59, 2F
討論串 (同標題文章)
完整討論串 (本文為第 1300 之 1553 篇):