-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueen_8.java
More file actions
89 lines (68 loc) · 2.07 KB
/
Queen_8.java
File metadata and controls
89 lines (68 loc) · 2.07 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
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
89
public class Queen_8 {
private static final int BOARD_SIZE = 8;
private static final char EMPTY = '-';
private static final char QUEEN = 'Q';
private char[][] board;
public Queen_8() {
board = new char[BOARD_SIZE][BOARD_SIZE];
initializeBoard();
}
private void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
public void solve() {
placeQueens(0);
printBoard();
}
private boolean placeQueens(int col) {
if (col >= BOARD_SIZE) {
return true; // All queens have been successfully placed
}
for (int row = 0; row < BOARD_SIZE; row++) {
if (isSafe(row, col)) {
board[row][col] = QUEEN;
if (placeQueens(col + 1)) {
return true;
}
board[row][col] = EMPTY; // Backtracking
}
}
return false; // Failed to place queens in the current column
}
private boolean isSafe(int row, int col) {
// Check row and column
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[row][i] == QUEEN || board[i][col] == QUEEN) {
return false;
}
}
// Check diagonals
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == QUEEN) {
return false;
}
}
for (int i = row, j = col; i < BOARD_SIZE && j >= 0; i++, j--) {
if (board[i][j] == QUEEN) {
return false;
}
}
return true;
}
public void printBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Queen_8 queens = new Queen_8();
queens.solve();
}
}