-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRatInAMazeAllPaths.java
More file actions
57 lines (45 loc) · 945 Bytes
/
RatInAMazeAllPaths.java
File metadata and controls
57 lines (45 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package allproblems;
public class PrintRatInAMaze {
public static void ratInMaze(int maze[][])
{
int n = maze.length;
int path[][]=new int[n][n];
printAllPaths(maze,0,0,path);
}
public static void printAllPaths(int maze[][],int i,int j, int path[][])
{
int n = maze.length;
if(i<0 || i>=n || j<0 || j>=n || maze[i][j]==0 || path[i][j]==1)
{
return;
}
path[i][j]=1;
if(i==n-1 && j==n-1)
{
for(int r = 0;r<n;r++)
{
for(int c = 0;c<n;c++)
{
System.out.print(path[r][c]+" ");
}
System.out.println();
}
System.out.println();
path[i][j]=0;
return;
}
//top
printAllPaths(maze,i-1,j,path);
//down
printAllPaths(maze, i+1, j, path);
//right
printAllPaths(maze,i,j+1,path);
//left
printAllPaths(maze, i, j-1, path);
path[i][j]=0;
}
public static void main(String[] args) {
int maze[][]= {{1,1,0},{1,1,0},{1,1,1}};
ratInMaze(maze);
}
}