675. Cut Off Trees for Golf Event

Problem: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. 0 represents the obstacle can't be reached.
  2. 1 represents the ground can be walked through.
  3. 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.
Analysis:
First sort all the trees by height. second cacl the distance from current cutted tree to next tree. Use priority queue to sort the trees and use bfs to calc distance.
Solution:
Check target when polling out of queue. If the target is the source itself, it can be found. 
class Solution {
    public int cutOffTree(List<List<Integer>> forest) {
        if (forest == null || forest.size() == 0) return -1;
        int m = forest.size();
        int n = forest.get(0).size();
           
        PriorityQueue<int[]> queue = new PriorityQueue<>((a,b) -> a[2] - b[2]);
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) {
                if (forest.get(i).get(j) > 1)
                    queue.offer(new int[]{i, j, forest.get(i).get(j)});
            }
         
         int targetRow = 0, targetCol = 0, res = 0;
         while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int step = bfs(forest, targetRow, targetCol, cur, m, n);
            if (step == -1)
                return -1;
             res += step;
             targetRow  = cur[0];
             targetCol  = cur[1];
         }
         return res;
    }
    
    private int bfs(List<List<Integer>> forest, int targetRow, int targetCol, int[] source, int m, int n) {
        Queue<int[]> queue = new LinkedList<>();
        int[][] dirs = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        boolean[][] visited = new boolean[m][n];
        queue.offer(source);
        visited[source[0]][source[1]] = true;
        int dis = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            
            for (int i = 0; i < size; i++) {
                int[] cur = queue.poll();
                if (cur[0] == targetRow && cur[1] == targetCol) return dis;
                for (int[] dir: dirs) {
                    int r = cur[0] + dir[0];
                    int c = cur[1] + dir[1];
                    if (r >= 0 && r < m && c >= 0 && c < n && forest.get(r).get(c) != 0 && !visited[r][c]) {
                        
                        queue.offer(new int[]{r, c});
                        visited[r][c] = true;
                    }
                }
            }
            dis++;
        }
        return -1;
    }
}

评论

此博客中的热门博文

776. Split BST

663. Equal Tree Partition

532. K-diff Pairs in an Array