675 Cut Off Trees for Golf Event
675 Cut Off Trees for Golf Event
1. Question
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:
0
represents theobstacle
can't be reached.1
represents theground
can be walked through.The place with number bigger than 1
represents atree
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).
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 twotrees
have the same height and there is at least one tree needs to be cut off.
Example 1:
Example 2:
Example 3:
Hint: size of the given matrix will not exceed 50x50.
2. Implementation
(1) PriorityQueue + BFS
思路:
3. Time & Space Complexity
PriorityQueue + BFS: 时间复杂度O(m^2n^2), 空间复杂度O(m^2n^2)
Last updated