|
| 1 | +package breadth_first_search; |
| 2 | +import java.util.*; |
| 3 | +/** |
| 4 | + * Created by gouthamvidyapradhan on 23/06/2018. |
| 5 | + * |
| 6 | + * You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, |
| 7 | + * in this map: |
| 8 | +
|
| 9 | + 0 represents the obstacle can't be reached. |
| 10 | + 1 represents the ground can be walked through. |
| 11 | + The place with number bigger than 1 represents a tree can be walked through, and this positive number represents the |
| 12 | + tree's height. |
| 13 | + 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 |
| 14 | + lowest height first. And after cutting, the original place has the tree will become a grass (value 1). |
| 15 | +
|
| 16 | + You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the |
| 17 | + trees. If you can't cut off all the trees, output -1 in that situation. |
| 18 | +
|
| 19 | + You are guaranteed that no two trees have the same height and there is at least one tree needs to be cut off. |
| 20 | +
|
| 21 | + Example 1: |
| 22 | + Input: |
| 23 | + [ |
| 24 | + [1,2,3], |
| 25 | + [0,0,4], |
| 26 | + [7,6,5] |
| 27 | + ] |
| 28 | + Output: 6 |
| 29 | + Example 2: |
| 30 | + Input: |
| 31 | + [ |
| 32 | + [1,2,3], |
| 33 | + [0,0,0], |
| 34 | + [7,6,5] |
| 35 | + ] |
| 36 | + Output: -1 |
| 37 | + Example 3: |
| 38 | + Input: |
| 39 | + [ |
| 40 | + [2,3,4], |
| 41 | + [0,0,5], |
| 42 | + [8,7,6] |
| 43 | + ] |
| 44 | + Output: 6 |
| 45 | + Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking. |
| 46 | + Hint: size of the given matrix will not exceed 50x50. |
| 47 | +
|
| 48 | +
|
| 49 | + Solution: O(N x M) ^ 2: Bfs to each height starting from 1 and calculate the total sum of distance. |
| 50 | + */ |
| 51 | +public class CutOffTreesForGolfEvent { |
| 52 | + |
| 53 | + public static void main(String[] args) throws Exception{ |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + private static final int[] R = {0, 0, 1, -1}; |
| 58 | + private static final int[] C = {1, -1, 0, 0}; |
| 59 | + |
| 60 | + static class Cell implements Comparable<Cell>{ |
| 61 | + int r, c; |
| 62 | + int distance; |
| 63 | + int height; |
| 64 | + Cell(int r, int c){ |
| 65 | + this.r = r; |
| 66 | + this.c = c; |
| 67 | + } |
| 68 | + @Override |
| 69 | + public int compareTo(Cell o) { |
| 70 | + return Integer.compare(this.height, o.height); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public int cutOffTree(List<List<Integer>> forest) { |
| 75 | + int distance = 0; |
| 76 | + List<Cell> trees = new ArrayList<>(); |
| 77 | + for(int i = 0; i < forest.size(); i ++){ |
| 78 | + for(int j = 0; j < forest.get(0).size(); j ++) { |
| 79 | + if(forest.get(i).get(j) > 1){ |
| 80 | + Cell cell = new Cell(i, j); |
| 81 | + cell.height = forest.get(i).get(j); |
| 82 | + trees.add(cell); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + Collections.sort(trees); |
| 87 | + int sR = 0, sC = 0; |
| 88 | + for(Cell t : trees){ |
| 89 | + int dist = bfs(forest, t.height, sR, sC); |
| 90 | + if(dist == -1) return -1; |
| 91 | + else distance += dist; |
| 92 | + sR = t.r; |
| 93 | + sC = t.c; |
| 94 | + } |
| 95 | + return distance; |
| 96 | + } |
| 97 | + |
| 98 | + private int bfs(List<List<Integer>> forest, int target, int sR, int sC){ |
| 99 | + if(forest.get(sR).get(sC) == target) { |
| 100 | + forest.get(sR).set(sC, 1); |
| 101 | + return 0; |
| 102 | + } |
| 103 | + Cell start = new Cell(sR, sC); |
| 104 | + start.distance = 0; |
| 105 | + Queue<Cell> queue = new ArrayDeque<>(); |
| 106 | + queue.add(start); |
| 107 | + boolean[][] done = new boolean[forest.size()][forest.get(0).size()]; |
| 108 | + done[sR][sC] = true; |
| 109 | + while(!queue.isEmpty()){ |
| 110 | + Cell cell = queue.poll(); |
| 111 | + for(int i = 0; i < 4; i ++){ |
| 112 | + int newR = cell.r + R[i]; |
| 113 | + int newC = cell.c + C[i]; |
| 114 | + Cell newCell = new Cell(newR, newC); |
| 115 | + if(newR >= 0 && newR < forest.size() && newC >= 0 && newC < forest.get(0).size() && |
| 116 | + forest.get(newR).get(newC) != 0 && !done[newCell.r][newCell.c]) { |
| 117 | + newCell.distance = cell.distance + 1; |
| 118 | + if(forest.get(newR).get(newC) == target){ |
| 119 | + forest.get(newR).set(newC, 1); |
| 120 | + return newCell.distance; |
| 121 | + } |
| 122 | + done[newCell.r][newCell.c] = true; |
| 123 | + queue.offer(newCell); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return -1; |
| 128 | + } |
| 129 | +} |
0 commit comments