forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 5
/
NQueenProblem.java
64 lines (54 loc) · 2.27 KB
/
NQueenProblem.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
/*
* Following code is the implementation of the N-queen problem.
* The solution is to place the queens on board such that no queen is under attack by another queen.
* There can be a number of possible solutions for a given length of board.
* This implementation prints only one valid solution, it can be extended to print all possible valid solutions.
*/
import java.util.Arrays;
public class NQueenProblem {
private static boolean isQueenSafe(int[][] board, int row, int column) {
int i;
int j;
int boardLength = board[0].length;
for (i = 0; i < column; ++i)
if (board[row][i] == 1)
return false; // If there's another Queen present in the same row.
for (i = row, j = column; i >= 0 && j >= 0; --i, --j)
if (board[i][j] == 1)
return false; // If there's another Queen present in the upper diagonal.
for (i = row, j = column; j >= 0 && i < boardLength; ++i, --j)
if (board[i][j]==1)
return false; // If there's another Queen present in the lower diagonal.
return true;
}
private static boolean solveNQueen(int[][] board, int column) {
int boardLength = board[0].length;
if (column >= boardLength)
return true;
for (int i = 0; i < boardLength; ++i) {
if (isQueenSafe(board, i, column)) {
board[i][column] = 1; // Place queen on (row, column) = (i, column).
if (solveNQueen(board, column + 1)) // Recurse for remaining board
return true;
else
board[i][column] = 0;
}
}
return false;
}
public static final boolean solveNQueen(int[][] board) {
return solveNQueen(board, 0);
}
public static void main(String[] args) {
int boardLength = 8;
int[][] board = new int[boardLength][boardLength];
if (!solveNQueen(board)) {
System.out.println("There is no possible answer for this boardLength");
} else {
System.out.println("A possible answer for given boardLength is:");
for (int i = 0; i < boardLength; ++i) {
System.out.println(Arrays.toString(board[i]));
}
}
}
}