-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNQueenSolver.java
64 lines (56 loc) · 1.92 KB
/
NQueenSolver.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
/*https://leetcode.com/problems/n-queens/*/
class Solution {
static List<List<String>> list;
static List<String> getSolution(boolean[][] board, int n)
{
List<String> temp = new ArrayList<String>();
for (int i = 0; i < n; ++i)
{
StringBuilder sb = new StringBuilder("");
for (int j = 0; j < n; ++j)
sb.append(board[i][j] ? 'Q' : '.');
temp.add(new String(sb));
}
return temp;
}
static boolean isSafe(boolean[][] board, int n, int r, int c)
{
//the row should be empty
for (int i = 0; i < c; ++i)
if (board[r][i]) return false;
//both the diagonals should be empty
for (int i = r, j = c; i >= 0 && j >= 0; --i, --j)
if (board[i][j]) return false;
for (int i = r, j = c; i < n && j >= 0; ++i, --j)
if (board[i][j]) return false;
return true;
}
static boolean solveBoard(boolean[][] board, int n, int c)
{
//if all columns checked, add the solution to the result
if (c >= n)
{
list.add(getSolution(board,n));
}
//for each row
for (int i = 0; i < n; ++i)
{
//check which row is safe for the column c
if (isSafe(board,n,i,c))
{
board[i][c] = true; //backtracking step - 1
//if the solution can be found for the next column, return true
if (solveBoard(board,n,c+1)) return true; //backtracking step - 2
board[i][c] = false; //backtracking step - 3
}
}
return false;
}
public List<List<String>> solveNQueens(int n) {
list = new ArrayList<List<String>>();
boolean[][] board = new boolean[n][n];
//start with column-0
solveBoard(board,n,0);
return list;
}
}