-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnqueens.java
More file actions
57 lines (55 loc) · 1.93 KB
/
nqueens.java
File metadata and controls
57 lines (55 loc) · 1.93 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
/*
* in this program we are going to learn reduce the time complexity of the program as it
* is we are going to eliminate the issafe function by creating three boolean arrays.
. . . . . . . Q
. . . Q . . . .
Q . . . . . . .
. . Q . . . . .
. . . . . Q . .
. Q . . . . . .
. . . . . . Q .
. . . . Q . . .
in the following there are 8 queens arranged and let us assume we are comming for first to last row.
so that we have to only check the column,left diagnol,right diagnol so that we create three boolean
array and back track them.
*/
public class nqueensopt{
public static void main(String [] args){
int n=8;
char[][] arr = new char[n][n];
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr.length; j++){
arr[i][j]='.';
}
}
nq(arr, 0, new boolean[n], new boolean[2*n-1], new boolean[2*n-1]);
}
public static void print(char[][] arr){
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr[0].length; j++){
System.out.print(arr[i][j] +" ");
}
System.out.println();
}
}
public static void nq(char[][] arr, int row, boolean[] cols, boolean[] d1, boolean[] d2){
if (row == arr.length){
print(arr);
System.out.println("----------------");
return;
}
for(int col=0; col<arr[0].length; col++){
if(cols[col]==false && d1[row + col]==false && d2[row - col + arr.length-1]==false){
arr[row][col]='Q';
cols[col]=true;
d1[row + col]=true;
d2[row - col + arr.length-1]=true;
nq(arr, row+1, cols, d1, d2);
arr[row][col]='.';
cols[col]=false;
d1[row + col]=false;
d2[row - col + arr.length-1]=false;
}
}
}
}