Cut Off Trees for Golf Event

LeetCode Q 675 - Cut Off Trees for Golf Event

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 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).
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:
Input:
[
[1,2,3],
[0,0,4],
[7,6,5]
]
Output: 6

Example 2:

Input:
[
[1,2,3],
[0,0,0],
[7,6,5]
]
Output: -1

Example 3:
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.

Solution : BFS + PriorityQueue

PriorityQueue: store each tree, order them according to their heights for low to high.

BFS: find min steps

Time complexity analysis:
The worst case time complexity could be O(m^2 * n^2) (m = number of rows, n = number of columns) since there are m * n trees and for each BFS worst case time complexity is O(m * n) too.

Code:

private static final int[][] DIRS = new int[][] { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
public int cutOffTree(List<List<Integer>> forest) {
	
	int R = forest.size(), C = forest.get(0).size();
	PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (a[2] - b[2]));

	// build the priorityqueue
	for (int r = 0; r < R; r++) {
		for (int c = 0; c < C; r++) {
			if (forest.get(r).get(c) > 0)
				pq.offer(new int[] {r, c, forest.get(r).get(c)}); 
		}
	}

	// bfs find the min step
	int sum = 0, sr = 0, sc = 0;
	while (!pq.isEmpty()) {
		int[] tree = pq.poll();
		int dist = getDistBFS(forest, sr, sc, tree[0], tree[1]);
		if (dist == -1) return -1;
		sum += dist;
		sr = tree[0]; sc = tree[1];
	}
	
	return sum;
}

private int getDistBFS(List<List<Integer>> forest, int sr, int sc, int tr, int tc) {
	int R = forest.size(), C = forest.get(0).size();
	boolean[][] visited = new boolean[R][C];
	visited[sr][sc] = true;

	Queue<int[]> que = new LinkedList();
	que.add(new int[]{sr, sc, 0});
	while (!que.isEmpty()) {
		int size = que.size();
		while (size-- != 0) {
			int[] curr = que.poll();
			if (curr[0] == tr && curr[1] == tc) return curr[2];
			for (int[] dir: DIRS) {
				int r = curr[0] + dir[0];
				int c = curr[1] + dir[1];

				if (r >= 0 && c >= 0 && r < R && c < C && !visited[r][c]) {
					que.offer(new int[] {r, c, curr[2] + 1});
					visited[r][c] = true;
				}
			}
		}
	}
	
	return -1;	
}

   Reprint policy


《Cut Off Trees for Golf Event》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC