Repository navigation
Expand file tree
/
Copy pathMainClass.java
More file actions
100 lines (76 loc) · 1.9 KB
/
Copy pathMainClass.java
File metadata and controls
100 lines (76 loc) · 1.9 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package backtracking;
public class MainClass {
public static boolean isSafe(int a[][], int n, int row, int col) {
for(int i = 0; i < n; i++) {
if(a[i][col] == 1) return false;
}
for(int i = row, j = col; i>=0 && j >=0; i--, j--) {
if(a[i][j] == 1) return false;
}
for(int i = row, j = col; i>=0 && j < n; i--, j++) {
if(a[i][j] == 1) return false;
}
return true;
}
public static boolean nQueen(int a[][], int n, int row) {
if(row == n) return true;
for(int col = 0; col < n; col++) {
if(isSafe(a, n, row, col)) {
a[row][col] = 1;
if(nQueen(a, n, row+1)) {
return true;
}
a[row][col] = 0;
}
}
return false;
}
public static boolean ratInAMaze(int a[][], boolean vis[][],
int i, int j) {
int n = a.length;
//Base conditions
if(i >= n || j >= n || a[i][j] == 0 || vis[i][j]) {
return false;
}
// Base condition for successfully reached
if(i == n-1 && j == n-1) return true;
// Mark this cell as visited
vis[i][j] = true;
// check if a path is possible from down
if(ratInAMaze(a, vis, i+1, j)) return true;
// check if a path is possible from right
if(ratInAMaze(a, vis, i, j+1)) return true;
// Mark this cell as unvisited & Backtrack
vis[i][j] = false;
// backtrack
return false;
}
public static void main(String[] args) {
int a[][] = {
{1, 1, 0, 1},
{1, 1, 1, 1},
{0, 0, 1, 0},
{1, 1, 1, 1}
};
boolean vis[][] = new boolean[a.length][a.length];
boolean isPathPossible = ratInAMaze(a, vis, 0, 0);
System.out.println(isPathPossible);
for(boolean [] array: vis) {
for(boolean e: array) {
System.out.print(e+" ");
}
System.out.println();
}
// int n = 5;
// int a[][] = new int[n][n];
//
// nQueen(a, n, 0);
//
// for(int[] array: a) {
// for(int e: array) {
// System.out.print(e+" ");
// }
// System.out.println();
// }
}
}