Skip to content

Commit c3f3edc

Browse files
Add files via upload
1 parent fd0d8cc commit c3f3edc

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Note:
3+
To get all the test cases accepted, make recursive calls in following order: Top, Down, Left, Right.
4+
This means that if the current cell is (x, y), then order of calls should be: top cell (x-1, y),
5+
down cell (x+1, y), left cell (x, y-1) and right cell (x, y+1).
6+
*/
7+
8+
public class Solution {
9+
10+
11+
static void ratInAMaze(int maze[][], int n) {
12+
/*
13+
* Your class should be named Solution.
14+
* Write your code here
15+
*/
16+
// n = maze.length;
17+
int path[][]=new int[n][n];
18+
printAllPaths(maze,0,0,path);
19+
}
20+
21+
public static void printAllPaths(int maze[][],int i,int j, int path[][])
22+
{
23+
int n = path.length;
24+
if(i<0 || i>=n || j<0 || j>=n || maze[i][j]==0 || path[i][j]==1)
25+
{
26+
return;
27+
}
28+
path[i][j]=1;
29+
30+
if(i==n-1 && j==n-1)
31+
{
32+
for(int r = 0;r<n;r++)
33+
{
34+
for(int c = 0;c<n;c++)
35+
{
36+
System.out.print(path[r][c]+" ");
37+
}
38+
// System.out.println();
39+
}
40+
System.out.println();
41+
path[i][j]=0;
42+
return;
43+
}
44+
45+
//top
46+
printAllPaths(maze,i-1,j,path);
47+
//down
48+
printAllPaths(maze, i+1, j, path);
49+
//right
50+
printAllPaths(maze,i,j+1,path);
51+
//left
52+
printAllPaths(maze, i, j-1, path);
53+
path[i][j]=0;
54+
55+
56+
57+
}
58+
59+
60+
61+
// public static void main(String[] args) {
62+
// int maze[][]= {{1,1,0},{1,1,0},{1,1,1}};
63+
// ratInAMaze(maze,3);
64+
65+
// }
66+
67+
}

BACKTRACKING/RAT IN A MAZE.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
public class Solution {
3+
4+
public static boolean ratInAMaze(int maze[][]){
5+
6+
/*Your class should be named Solution.
7+
*Don't write main().
8+
*Don't take input, it is passed as function argument.
9+
*Don't print output.
10+
*Taking input and printing output is handled automatically.
11+
*/
12+
13+
int n = maze.length; //row length
14+
int path[][]= new int[n][n];
15+
return solveMaze(maze, 0, 0, path);
16+
}
17+
18+
public static boolean solveMaze(int maze[][],int i,int j, int path[][])
19+
{
20+
int n = maze.length;
21+
//checking if the cell is a valid cell or not
22+
if(i<0 || i>=n || j<0 || j>=n || maze[i][j]==0 || path[i][j]==1)
23+
{
24+
return false;
25+
}
26+
//include cell in current path
27+
path[i][j]=1;
28+
29+
//destination cell
30+
if(i==n-1 && j==n-1)
31+
{
32+
return true;
33+
}
34+
35+
//explore in all directions
36+
//top
37+
if(solveMaze(maze, i-1, j, path))
38+
{
39+
return true;
40+
}
41+
//right
42+
if(solveMaze(maze, i, j+1, path))
43+
{
44+
return true;
45+
}
46+
//down
47+
if(solveMaze(maze, i+1, j, path))
48+
{
49+
return true;
50+
}
51+
//left
52+
if(solveMaze(maze, i, j-1, path))
53+
{
54+
return true;
55+
}
56+
57+
return false;
58+
59+
60+
}
61+
62+
public static void main(String[] args) {
63+
int maze[][]= {{1,1,0},{1,1,0},{1,1,1}};
64+
boolean pathpossible = ratInAMaze(maze);
65+
System.out.println("Path exists?: "+pathpossible);
66+
}
67+
68+
}

0 commit comments

Comments
 (0)