-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatInaMaze_Bactrack.java
54 lines (49 loc) · 1.29 KB
/
RatInaMaze_Bactrack.java
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
import java.util.Scanner;
//maze program to understand backtracking
class Ratinamaze {
public static Boolean positionPossible(int[][] maze, int x, int y) {
if(maze[x][y] == 1)
return true;
else
return false;
}
public static void solveMaze(int[][] maze, int x, int y, int[][] sol) {
if (x + 1 < maze.length && positionPossible(maze,x+1,y)) {
sol[x+1][y] = 1;
solveMaze(maze,x+1,y,sol);
}
else if (y + 1 < maze.length && positionPossible(maze,x,y+1)) {
sol[x][y+1] = 1;
solveMaze(maze,x,y+1,sol);
}
else if (x == maze.length - 1 && y == maze.length - 1)
return;
else
return;
}
public static void printSol(int[][] sol, int row) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < row; j++) {
System.out.print(sol[i][j] + " ");
}
System.out.println("");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int[][] maze = new int[row][row];
for ( int i = 0; i < row; i++) {
for ( int j = 0; j < row; j++) {
maze[i][j] = sc.nextInt();
}
}
// System.out.print(maze.length);
int[][] sol = new int[row][row];
sol[0][0] = 1;
Ratinamaze rat = new Ratinamaze();
rat.solveMaze(maze,0,0,sol);
System.out.println("///path matrix is as follows///");
rat.printSol(sol, row);
}
}