-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNQueens.java
88 lines (75 loc) · 2.26 KB
/
NQueens.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
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
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
// complexity is b ^ h ; in here TC is n! * n otherwise n ^ n
//SC is O(n) + o(n) + n! * n
//[[
//"--q-",
//"---q",
//"---q",
//"-q--"
//], [
//"-q--",
//"---q",
//"q---",
//"--q-"
//]]
public class NQueens {
public static List<List<String>> result = new ArrayList<>();
public static int counter = 0;
public static String[][] solveNQueens(int n) {
List<Integer> board = new ArrayList<>(n);
for(int i = 0; i < n; i++)
board.add(i);
solveNQueensHelper(0, board);
String[][] s = new String[result.size()][n];
for(int i=0; i <result.size(); i++) {
for(int j =0; j <n; j++) {
s[i][j]=result.get(i).get(j);
}
}
return s;
}
private static void solveNQueensHelper(int i, List<Integer> board) {
if (i == board.size()) {
counter++;
List<String> l = new ArrayList<>();
for(int k = 0; k < board.size(); k++) {
int col = board.get(k);
StringBuilder sb = new StringBuilder();
for(int e = 0; e < board.size(); e++) {
if(e!= col)
sb.append("-");
else
sb.append("q");
}
l.add(sb.toString());
}
result.add(l);
}
for (int j = i; j < board.size(); j++) {
swapMe(board, i, j);
if(isValid(i, board))
solveNQueensHelper(i + 1, board);
swapMe(board, i, j);
}
}
private static boolean isValid(int i, List<Integer> board) {
for (int c = 0; c < i; c++) {
int rowDiff = Math.abs(i - c);
int colDiff = Math.abs(board.get(i) - board.get(c));
if (rowDiff == colDiff) return false;
}
return true;
}
private static void swapMe(List<Integer> board, int idx1, int idx2) {
int temp = board.get(idx1);
board.set(idx1,board.get(idx2));
board.set(idx2,temp);
}
public static void main(String[] args) {
solveNQueens(8);
System.out.println(result);
System.out.println(counter);
}
}