forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEightQueens.java
66 lines (50 loc) · 1.65 KB
/
EightQueens.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
package dynamic;
import utils.PrintUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class EightQueens {
private static final int GRID = 4;
public static void main(String[] args) {
Integer[] board = new Integer[GRID];
Arrays.fill(board, 0);
List<Integer[]> result = new ArrayList<>();
placeQueens(0, board, result);
PrintUtils.printArr(result);
}
static void placeQueens(int row, Integer[] board1D, List<Integer[]> results) {
if (row == GRID) {
results.add(board1D.clone());
} else {
for (int col = 0; col < GRID; col++) {
if (checkValid(board1D, row, col)) {
board1D[row] = col;
placeQueens(row + 1, board1D, results);
}
}
}
}
/**
* We have represented the 2D chess board as 1D so
* index ----> row
* column ---> board1d[row]
*/
private static boolean checkValid(Integer[] board1D, int row1, int col1) {
// row2 in 0..row1 because rows beyond row1 has not been filled yet.
for (int row2 = 0; row2 < row1; row2++) {
// Getting column by using formula
int col2 = board1D[row2];
if (col1 == col2) {
return false;
}
// Diagonal check
int colDistance = Math.abs(col1 - col2);
int rowDistance = Math.abs(row1 - row2);
// in diagonal, row & column are equal.
if (colDistance == rowDistance) {
return false;
}
}
return true;
}
}