|
| 1 | +/* |
| 2 | +You are given a N*N maze with a rat placed at maze[0][0]. Find and print all paths 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 | +The first line of input contains an integer 'N' representing the dimension of the maze. |
| 7 | +The next N lines of input contain N space-separated integers representing the type of the cell. |
| 8 | + |
| 9 | +Output Format : |
| 10 | +For each test case, print the path from start position to destination position and only cells that are part of the solution path should be 1, rest all cells should be 0. |
| 11 | +Output for every test case will be printed in a separate line. |
| 12 | +Note: |
| 13 | +You do not need to print anything, it has already been taken care of. Just implement the given function. |
| 14 | + |
| 15 | +Constraints: |
| 16 | +0 < N < 11 0 <= Maze[i][j] <=1 |
| 17 | +Time Limit: 1sec |
| 18 | + |
| 19 | +Sample Input 1 : |
| 20 | +3 |
| 21 | +1 0 1 |
| 22 | +1 0 1 |
| 23 | +1 1 1 |
| 24 | +Sample Output 1 : |
| 25 | +1 0 0 1 0 0 1 1 1 |
| 26 | +Sample Output 1 Explanation : |
| 27 | +Only 1 path is possible |
| 28 | + |
| 29 | +1 0 0 |
| 30 | +1 0 0 |
| 31 | +1 1 1 |
| 32 | +Which is printed from left to right and then top to bottom in one line. |
| 33 | + |
| 34 | +Sample Input 2 : |
| 35 | +3 |
| 36 | +1 0 1 |
| 37 | +1 1 1 |
| 38 | +1 1 1 |
| 39 | +Sample Output 2 : |
| 40 | +1 0 0 1 1 1 1 1 1 |
| 41 | +1 0 0 1 0 0 1 1 1 |
| 42 | +1 0 0 1 1 0 0 1 1 |
| 43 | +1 0 0 1 1 1 0 0 1 |
| 44 | + |
| 45 | +Sample Output 2 Explanation : |
| 46 | +4 paths are possible which are printed in the required format. |
| 47 | +*/ |
| 48 | + |
| 49 | +public class Solution { |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + public static void ratInAMaze(int maze[][]){ |
| 54 | + |
| 55 | + /* Your class should be named Solution. |
| 56 | + * Don't write main() function. |
| 57 | + * Don't read input, it is passed as function argument. |
| 58 | + * Print output as specified in the question |
| 59 | + */ |
| 60 | + int path[][] = new int[maze.length][maze.length]; |
| 61 | + solveMaze(maze,0,0,path); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + public static void solveMaze(int[][] maze, int i, int j, int[][] path) |
| 66 | + { |
| 67 | + //Check if i,j are valid pair of indices => i,j>=0 |
| 68 | + int n=maze.length; |
| 69 | + if (i<0 || j<0 || i>=n || j>=n) |
| 70 | + return; |
| 71 | + |
| 72 | + //If cell is already part of the path |
| 73 | + if (path[i][j]==1) |
| 74 | + return; |
| 75 | + |
| 76 | + //If cell is blocked in maze (cell value=0) |
| 77 | + if (maze[i][j]==0) |
| 78 | + return; |
| 79 | + |
| 80 | + //If all previous conditions fail, then the cell is a possible path |
| 81 | + //Include the cell in current path |
| 82 | + path[i][j]=1; |
| 83 | + |
| 84 | + //If we have reached ending point |
| 85 | + if (i==n-1 && j==n-1) |
| 86 | + { |
| 87 | + for (int row=0;row<n;row++) |
| 88 | + { |
| 89 | + for (int col=0;col<n;col++) |
| 90 | + { |
| 91 | + System.out.print(path[row][col]+" "); |
| 92 | + } |
| 93 | + |
| 94 | + //System.out.println(); |
| 95 | + } |
| 96 | + path[i][j]=0; |
| 97 | + System.out.println(); |
| 98 | + return; |
| 99 | + } |
| 100 | + //Changing the recursive calls to find all the paths |
| 101 | + solveMaze(maze,i-1,j,path); |
| 102 | + solveMaze(maze,i,j+1,path); |
| 103 | + solveMaze(maze,i+1,j,path); |
| 104 | + solveMaze(maze,i,j-1,path); |
| 105 | + path[i][j]=0; |
| 106 | + |
| 107 | + |
| 108 | + //If none of the conditions are satisfied, then the path is not working out |
| 109 | + return; |
| 110 | + } |
| 111 | +} |
| 112 | + |
0 commit comments