|
| 1 | +/* |
| 2 | +You are given a N*N maze with a rat placed at maze[0][0]. Find whether any path exist that rat can follow to reach its destination i.e. maze[N-1][N-1]. Rat can move in any direction ( left, right, up and down). |
| 3 | +Value of every cell in the maze can either be 0 or 1. Cells with value 0 are blocked means rat cannot enter into those cells and those with value 1 are open. |
| 4 | + |
| 5 | +Input Format |
| 6 | +Line 1: Integer N |
| 7 | +Next N Lines: Each line will contain ith row elements (separated by space) |
| 8 | + |
| 9 | +Output Format : |
| 10 | +The output line contains true if any path exists for the rat to reach its destination otherwise print false. |
| 11 | + |
| 12 | +Sample Input 1 : |
| 13 | +3 |
| 14 | +1 0 1 |
| 15 | +1 0 1 |
| 16 | +1 1 1 |
| 17 | +Sample Output 1 : |
| 18 | +true |
| 19 | + |
| 20 | +Sample Input 2 : |
| 21 | +3 |
| 22 | +1 0 1 |
| 23 | +1 0 1 |
| 24 | +0 1 1 |
| 25 | +Sample Output 2 : |
| 26 | +false |
| 27 | +*/ |
| 28 | + |
| 29 | +public class Solution { |
| 30 | + |
| 31 | + public static boolean ratInAMaze(int maze[][]){ |
| 32 | + |
| 33 | + /*Your class should be named Solution. |
| 34 | + *Don't write main(). |
| 35 | + *Don't take input, it is passed as function argument. |
| 36 | + *Don't print output. |
| 37 | + *Taking input and printing output is handled automatically. |
| 38 | + */ |
| 39 | + int path[][] = new int[maze.length][maze.length]; |
| 40 | + return solveMaze(maze,0,0,path); |
| 41 | + } |
| 42 | + |
| 43 | + public static boolean solveMaze(int[][] maze, int i, int j, int[][] path) |
| 44 | + { |
| 45 | + //Check if i,j are valid pair of indices => i,j>=0 |
| 46 | + int n=maze.length; |
| 47 | + if (i<0 || j<0 || i>=n || j>=n) |
| 48 | + return false; |
| 49 | + |
| 50 | + //If cell is already part of the path |
| 51 | + if (path[i][j]==1) |
| 52 | + return false; |
| 53 | + |
| 54 | + //If cell is blocked in maze (cell value=0) |
| 55 | + if (maze[i][j]==0) |
| 56 | + return false; |
| 57 | + |
| 58 | + //If all previous conditions fail, then the cell is a possible path |
| 59 | + //Include the cell in current path |
| 60 | + path[i][j]=1; |
| 61 | + |
| 62 | + //If we have reached ending point |
| 63 | + if (i==n-1 && j==n-1) |
| 64 | + return true; |
| 65 | + |
| 66 | + //Now, explore in all directions |
| 67 | + // Direction 1 - move towards cell above (top direction) |
| 68 | + if (solveMaze(maze,i-1,j,path)) |
| 69 | + return true; |
| 70 | + |
| 71 | + //Direction 2 - move towards cell to the right (right direction) |
| 72 | + if (solveMaze(maze,i,j+1,path)) |
| 73 | + return true; |
| 74 | + |
| 75 | + //Direction 3 - move towards cell below (bottom direction) |
| 76 | + if (solveMaze(maze,i+1,j,path)) |
| 77 | + return true; |
| 78 | + |
| 79 | + //Direction 3 - move towards cell to the left (left direction) |
| 80 | + if (solveMaze(maze,i,j-1,path)) |
| 81 | + return true; |
| 82 | + |
| 83 | + //If none of the conditions are satisfied, then the path is not working out |
| 84 | + return false; |
| 85 | + } |
| 86 | +} |
0 commit comments