-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNQueens.java
50 lines (49 loc) · 1.6 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
import java.util.Scanner;
public class NQueens {
private static boolean isSafe(int[][] board, int row, int col, int N) {
for (int i = 0; i < col; i++) {
if (board[row][i] == 1) return false;
}
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 1) return false;
}
for (int i = row, j = col; i < N && j >= 0; i++, j--) {
if (board[i][j] == 1) return false;
}
return true;
}
private static boolean solveNQueens(int[][] board, int col, int N) {
if (col >= N) {
printSolution(board, N);
return true;
}
boolean res = false;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col, N)) {
board[i][col] = 1;
res = solveNQueens(board, col + 1, N) || res;
board[i][col] = 0;
}
}
return res;
}
private static void printSolution(int[][] board, int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print((board[i][j] == 1 ? "Q " : ". "));
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the board (N): ");
int N = sc.nextInt();
int[][] board = new int[N][N];
if (!solveNQueens(board, 0, N)) {
System.out.println("No solution exists for " + N + " queens.");
}
sc.close();
}
}