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:
  1. 1.
    0represents theobstaclecan't be reached.
  2. 2.
    1represents thegroundcan be walked through.
  3. 3.
    The place with number bigger than 1represents 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 twotreeshave the same height and there is at least one tree needs to be cut off.
Example 1:
1
Input:
2
3
[
4
[1,2,3],
5
[0,0,4],
6
[7,6,5]
7
]
8
9
Output: 6
Copied!
Example 2:
1
Input:
2
[
3
[1,2,3],
4
[0,0,0],
5
[7,6,5]
6
]
7
8
Output: -1
Copied!
Example 3:
1
Input:
2
3
[
4
[2,3,4],
5
[0,0,5],
6
[8,7,6]
7
]
8
9
Output: 6
10
11
Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking.
Copied!
Hint: size of the given matrix will not exceed 50x50.

2. Implementation

(1) PriorityQueue + BFS
思路:
1
class Solution {
2
public int cutOffTree(List<List<Integer>> forest) {
3
if (forest.size() == 0 && forest.get(0).size() == 0) {
4
return -1;
5
}
6
7
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b)->(a[2] - b[2]));
8
int m = forest.size(), n = forest.get(0).size();
9
10
for (int i = 0; i < m; i++) {
11
for (int j = 0; j < n; j++) {
12
if (forest.get(i).get(j) > 1) {
13
pq.add(new int[] {i, j, forest.get(i).get(j)});
14
}
15
}
16
}
17
18
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
19
int[] start = {0, 0};
20
int res = 0;
21
while (!pq.isEmpty()) {
22
int[] destination = pq.remove();
23
int steps = getStepsByBFS(start, destination, m, n, directions, forest);
24
25
if (steps < 0) {
26
return -1;
27
}
28
29
res += steps;
30
start[0] = destination[0];
31
start[1] = destination[1];
32
}
33
return res;
34
}
35
36
public int getStepsByBFS(int[] start, int[] dest, int m, int n, int[][] directions, List<List<Integer>> forest) {
37
boolean[][] visited = new boolean[m][n];
38
Queue<int[]> queue = new LinkedList<>();
39
queue.add(start);
40
visited[start[0]][start[1]] = true;
41
int steps = 0;
42
43
while (!queue.isEmpty()) {
44
int size = queue.size();
45
for (int i = 0; i < size; i++) {
46
int[] curCell = queue.remove();
47
48
if (curCell[0] == dest[0] && curCell[1] == dest[1]) {
49
return steps;
50
}
51
52
for (int[] direction : directions) {
53
int nextRow = curCell[0] + direction[0];
54
int nextCol = curCell[1] + direction[1];
55
56
if (isValid(m, n, nextRow, nextCol, visited, forest)) {
57
queue.add(new int[] {nextRow, nextCol});
58
visited[nextRow][nextCol] = true;
59
}
60
}
61
}
62
++steps;
63
}
64
return -1;
65
}
66
67
public boolean isValid(int rows, int cols, int curRow, int curCol, boolean[][] visited, List<List<Integer>> forest) {
68
return curRow >= 0 && curRow < rows && curCol >= 0 && curCol < cols && !visited[curRow][curCol] && forest.get(curRow).get(curCol) > 0;
69
}
70
}
Copied!

3. Time & Space Complexity

PriorityQueue + BFS: 时间复杂度O(m^2n^2), 空间复杂度O(m^2n^2)