-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNQueen.java
More file actions
109 lines (88 loc) · 2.73 KB
/
NQueen.java
File metadata and controls
109 lines (88 loc) · 2.73 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// N Queen Problem | Backtracking
// The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other.
// https://leetcode.com/problems/n-queens/
package RecursionBacktracking.level3;
import java.util.ArrayList;
import java.util.List;
public class NQueen {
public static List<List<String>> solveNQue(int n) {
List<List<String>> allBoard = new ArrayList<>();
char[][] board = new char[n][n];
helper(board, allBoard, 0);
return allBoard;
}
static void helper(char[][] board, List<List<String>> allBoard, int col) {
if (board.length == 0) {
saveBoard(board, allBoard);
return;
}
for (int row = 0; row < board.length; row++) {
if (isSafe(row, col, board)) {
board[row][col] = 'Q';
helper(board, allBoard, col + 1);
board[row][col] = '.';
}
}
}
static void saveBoard(char[][] board, List<List<String>> allBoard) {
String row = "";
List<String> newBoard = new ArrayList<>();
for (int i = 0; i < board.length; i++) {
row = "";
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'Q') {
row += "Q";
} else {
row += ".";
}
}
newBoard.add(row);
}
allBoard.add(newBoard);
}
static boolean isSafe(int row, int col, char[][] board) {
// vertical
for (int i = 0; i < board.length; i++) {
if (board[i][col] == 'Q') {
return false;
}
}
// horizontal
for (int j = 0; j < board.length; j++) {
if (board[row][j] == 'Q') {
return false;
}
}
// top left
int r = row;
for (int c = col; c >= 0 && r >= 0; c--, r--) {
if (board[r][c] == 'Q') {
return false;
}
}
// top right
r = row;
for (int c = col; c < board.length && r >= 0; r--, c++) {
if (board[r][c] == 'Q') {
return false;
}
}
// bottom left
r = row;
for (int c = col; c >= 0 && r < board.length; r++, c--) {
if (board[r][c] == 'Q') {
return false;
}
}
// lower right
for (int c = col; c < board.length && r < board.length; c++, r++) {
if (board[r][c] == 'Q') {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(solveNQue(4));
}
}