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 twotreeshave 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.
classSolution {publicintcutOffTree(List<List<Integer>> forest) {// Get all the trees with h-height, r-row, c-column and sort by heightList<int[]> trees =newArrayList<>();int[][] map =newint[forest.size()][forest.get(0).size()];for (int r =0; r <forest.size(); r++) {for (int c =0; c <forest.get(r).size(); c++) {int h =forest.get(r).get(c); map[r][c] = h;if (h >1) {trees.add(newint[] {h, r, c}); } } }// Sort trees based on height of treesCollections.sort(trees, (t1, t2) ->Integer.compare(t1[0], t2[0]));int ans =0;int r0 =0;int c0 =0;// Find distance from current position to next treefor (int[] tree: trees) {int d =dist(map, r0, c0, tree[1], tree[2]);if (d <0) {return-1; } ans += d; r0 = tree[1]; c0 = tree[2]; }return ans; }privatestaticint[][] dirs = {{-1,0}, {1,0} , {0,-1}, {0,1}};privateintdist(int[][] map,int sr,int sc,int tr,int tc) {int distance =0;boolean[][] visited =newboolean[map.length][map[0].length];Queue<int[]> q =newLinkedList<int[]>();q.offer(newint[] {sr, sc}); visited[sr][sc] =true;while (!q.isEmpty()) {int levelSize =q.size();while (levelSize-->0) {int[] pos =q.poll();if (pos[0] == tr && pos[1] == tc) {return distance; }for (int[] dir: dirs) {int nr = pos[0] + dir[0];int nc = pos[1] + dir[1];if (nr >=0&& nr <map.length&& nc >=0&& nc < map[0].length) {if (map[nr][nc] !=0&&!visited[nr][nc]) {q.offer(newint[] {nr, nc}); visited[nr][nc] =true; } } } } distance++; }return-1; }}
Another BFS (Store distance in int[] {r, c, dist})
类似上面的一层一层BFS,这里就是把距离放到每个节点的int[]中.
Runtime: 258 ms, faster than 84.21% of Java online submissions for Cut Off Trees for Golf Event.
Memory Usage: 46.7 MB, less than 100.00% of Java online submissions for Cut Off Trees for Golf Event.
classSolution {publicintcutOffTree(List<List<Integer>> forest) {// Get all the trees with h-height, r-row, c-column and sort by heightList<int[]> trees =newArrayList<>();int[][] map =newint[forest.size()][forest.get(0).size()];for (int r =0; r <forest.size(); r++) {for (int c =0; c <forest.get(r).size(); c++) {int h =forest.get(r).get(c); map[r][c] = h;if (h >1) {trees.add(newint[] {h, r, c}); } } }// Sort trees based on height of treesCollections.sort(trees, (t1, t2) ->Integer.compare(t1[0], t2[0]));int ans =0;int r0 =0;int c0 =0;// Find distance from current position to next treefor (int[] tree: trees) {int d =dist(forest, r0, c0, tree[1], tree[2]);if (d <0) {return-1; } ans += d; r0 = tree[1]; c0 = tree[2]; }return ans; }privatestaticint[][] dirs = {{-1,0}, {1,0} , {0,-1}, {0,1}};publicintdist(List<List<Integer>> forest,int sr,int sc,int tr,int tc) {int R =forest.size(), C =forest.get(0).size();Queue<int[]> queue =newLinkedList();boolean[][] seen =newboolean[R][C];queue.add(newint[]{sr, sc,0}); seen[sr][sc] =true;while (!queue.isEmpty()) {int[] cur =queue.poll();if (cur[0] == tr && cur[1] == tc) {return cur[2]; }for (int[] dir: dirs) {int r = cur[0] + dir[0];int c = cur[1] + dir[1];if (0<= r && r < R &&0<= c && c < C &&!seen[r][c] &&forest.get(r).get(c) >0) { seen[r][c] =true;queue.add(newint[]{r, c, cur[2] +1}); } } }return-1; }}