★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-nbaubuxi-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map:git
0
represents the obstacle
can't be reached.1
represents the ground
can be walked through.The place with number bigger than 1
represents a tree
can be walked through, and this positive number represents the tree's height.You are asked to cut off all the trees in this forest in the order of tree's height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).github
You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees. If you can't cut off all the trees, output -1 in that situation.數組
You are guaranteed that no two trees
have the same height and there is at least one tree needs to be cut off.微信
Example 1:app
Input: [ [1,2,3], [0,0,4], [7,6,5] ] Output: 6
Example 2:this
Input: [ [1,2,3], [0,0,0], [7,6,5] ] Output: -1
Example 3:spa
Input: [ [2,3,4], [0,0,5], [8,7,6] ] Output: 6 Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking.
Hint: size of the given matrix will not exceed 50x50.rest
你被請來給一個要舉辦高爾夫比賽的樹林砍樹. 樹林由一個非負的二維數組表示, 在這個數組中:code
0
表示障礙,沒法觸碰到.1
表示能夠行走的地面.比1大的數
表示一顆容許走過的樹的高度.你被要求按照樹的高度從低向高砍掉全部的樹,每砍過一顆樹,樹的高度變爲1。
你將從(0,0)點開始工做,你應該返回你砍完全部樹須要走的最小步數。 若是你沒法砍完全部的樹,返回 -1 。
能夠保證的是,沒有兩棵樹的高度是相同的,而且至少有一顆樹須要你砍。
示例 1:
輸入: [ [1,2,3], [0,0,4], [7,6,5] ] 輸出: 6
示例 2:
輸入: [ [1,2,3], [0,0,0], [7,6,5] ] 輸出: -1
示例 3:
輸入: [ [2,3,4], [0,0,5], [8,7,6] ] 輸出: 6 解釋: (0,0) 位置的樹,你能夠直接砍去,不用算步數
提示: 矩陣大小不會超過 50x50 。
Runtime: 7864 ms
1 class Solution { 2 var dirs:[[Int]] = [[0,1],[0, -1],[1, 0],[-1, 0]] 3 func cutOffTree(_ forest: [[Int]]) -> Int { 4 var forest = forest 5 //forest.sort(by:{return $0[2] < $1[2]}) 6 var pq:[[Int]] = [[Int]]() 7 for r in 0..<forest.count 8 { 9 for c in 0..<forest[0].count 10 { 11 var height:Int = forest[r][c] 12 if height > 0 13 { 14 pq.append([r, c, height]) 15 } 16 } 17 } 18 pq.sort(by:{return $0[2] < $1[2]}) 19 var last_i:Int = 0 20 var last_j:Int = 0 21 var result:Int = 0 22 while (!pq.isEmpty) 23 { 24 var new_step:[Int] = pq.removeFirst() 25 var step:Int = bfs(&forest, last_i, last_j, new_step[0], new_step[1]) 26 if step == -1 {return -1} 27 result += step 28 last_i = new_step[0] 29 last_j = new_step[1] 30 } 31 return result 32 } 33 34 func bfs(_ forest:inout [[Int]],_ start_i:Int,_ start_j:Int,_ end_i:Int,_ end_j:Int) -> Int 35 { 36 var queue:[[Int]] = [[Int]]() 37 var m:Int = forest.count 38 var n:Int = forest[0].count 39 var visited:[[Bool]] = [[Bool]](repeating:[Bool](repeating:false,count:n),count:m) 40 queue.append([start_i, start_j]) 41 var level:Int = 0 42 while(!queue.isEmpty) 43 { 44 var size:Int = queue.count 45 for i in 0..<size 46 { 47 var curr:[Int] = queue.removeFirst() 48 if curr[0] == end_i && curr[1] == end_j 49 { 50 return level 51 } 52 for j in 0..<4 53 { 54 var next_i:Int = curr[0]+dirs[j][0] 55 var next_j:Int = curr[1]+dirs[j][1] 56 if inBounds(forest, next_i , next_j) && !visited[next_i][next_j] 57 { 58 visited[next_i][next_j] = true 59 queue.append([next_i, next_j]) 60 } 61 } 62 } 63 level += 1 64 } 65 return -1 66 } 67 68 func inBounds(_ forest: [[Int]],_ i:Int,_ j:Int) -> Bool 69 { 70 return (i >= 0) && (i <= forest.count-1) 71 && (j >= 0) && (j <= forest[0].count - 1) && (forest[i][j] != 0) 72 } 73 }