|
| 1 | +package com.eprogrammerz.examples.algorithm.crackingCoding; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Queue; |
| 8 | + |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | + |
| 11 | +/** |
| 12 | + * 1 -> valid grid |
| 13 | + * 0 -> barrier |
| 14 | + * 9 -> obstacle |
| 15 | + * Find the shortest path for robot to get to the obstacle starting from top left most grid |
| 16 | + */ |
| 17 | +public class ObstacleRemoval { |
| 18 | + public int findShortestPath(int rowNum, int colNum, int[][] lot) { |
| 19 | + boolean[][] visited = new boolean[rowNum][colNum]; |
| 20 | + int[][] distances = new int[rowNum][colNum]; |
| 21 | + |
| 22 | + Queue<Pair> queue = new LinkedList<>(); |
| 23 | + queue.add(new Pair(0,0)); |
| 24 | + distances[0][0] = 0; |
| 25 | + while (!queue.isEmpty()) { |
| 26 | + Pair pair = queue.poll(); |
| 27 | + int x = pair.x; |
| 28 | + int y = pair.y; |
| 29 | + |
| 30 | + int distance = distances[x][y]; |
| 31 | + |
| 32 | + if (lot[x][y] == 9) return distance; |
| 33 | + |
| 34 | + visited[x][y] = true; |
| 35 | + |
| 36 | + if (isValid(x + 1, y, rowNum, colNum, visited, lot)) { |
| 37 | + queue.add(new Pair(x + 1, y)); |
| 38 | + distances[x + 1][y] = distances[x][y] + 1; |
| 39 | + } |
| 40 | + |
| 41 | + if (isValid(x, y + 1, rowNum, colNum, visited, lot)) { |
| 42 | + queue.add(new Pair(x , y +1)); |
| 43 | + distances[x][y + 1] = distances[x][y] + 1; |
| 44 | + } |
| 45 | + |
| 46 | + if (isValid(x - 1, y, rowNum, colNum, visited, lot)) { |
| 47 | + queue.add(new Pair(x - 1, y)); |
| 48 | + distances[x - 1][y] = distances[x][y] + 1; |
| 49 | + } |
| 50 | + |
| 51 | + if (isValid(x, y - 1, rowNum, colNum, visited, lot)) { |
| 52 | + queue.add(new Pair(x, y - 1)); |
| 53 | + distances[x][y - 1] = distances[x][y] + 1; |
| 54 | + } |
| 55 | + } |
| 56 | + return -1; |
| 57 | + } |
| 58 | + |
| 59 | + private boolean isValid(int x, int y, int rowNum, int colNum, boolean[][] visited, int[][] lot) { |
| 60 | + if (x >= 0 && x < rowNum && y >= 0 && y < colNum && !visited[x][y] && (lot[x][y] == 1 || lot[x][y] == 9)) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + private class Pair { |
| 67 | + int x; |
| 68 | + int y; |
| 69 | + Pair(int x, int y) { |
| 70 | + this.x = x; |
| 71 | + this.y = y; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String toString() { |
| 76 | + return "(" + x + ", " + y + ")"; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testShortestPath() { |
| 82 | + int[][] lot1 = new int[][] { |
| 83 | + {1,0,0}, |
| 84 | + {1,0,0}, |
| 85 | + {1,9,0} |
| 86 | + }; |
| 87 | + assertEquals(3, findShortestPath(3,3,lot1)); |
| 88 | + |
| 89 | + int[][] lot2 = new int[][] { |
| 90 | + {1,0,0}, |
| 91 | + {1,1,0}, |
| 92 | + {1,1,9} |
| 93 | + }; |
| 94 | + assertEquals(4, findShortestPath(3,3,lot2)); |
| 95 | + |
| 96 | + int[][] lot3 = new int[][] { |
| 97 | + {1,1,1,1}, |
| 98 | + {1,1,0,1}, |
| 99 | + {1,0,0,9}, |
| 100 | + {1,1,1,1} |
| 101 | + }; |
| 102 | + assertEquals(5, findShortestPath(4,4,lot3)); |
| 103 | + } |
| 104 | +} |
0 commit comments