-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathNQueen.java
68 lines (66 loc) · 1.81 KB
/
NQueen.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
65
66
67
68
//this is the classic Recursion Problem NQueen
//problem link: https://leetcode.com/problems/n-queens/
package Recursion.Combo;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class NQueen {
public List<List<String>> solveNQueens(int n) {
char [][] board = new char[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
board[i][j]='.';
}
}
List<List<String>> ans = new ArrayList<>();
solve(0,board,ans);
return ans;
}
public void solve(int col, char[][]board, List<List<String>> ans){
if(col==board.length){
ans.add(construct(board));
return;
}
for(int row=0;row<board.length;row++){
if(isValid(board,row,col)){
board[row][col]='Q';
solve(col+1,board,ans);
board[row][col]='.';
}
}
}
public List<String> construct(char [][]board){
List<String> res = new LinkedList<>();
for(int i=0;i<board.length;i++){
String s = new String(board[i]);
res.add(s);
}
return res;
}
public boolean isValid(char[][]board, int row, int col){
int duprow = row;
int dupcol = col;
//upper diagonal
while(row>=0 && col>=0){
if(board[row][col]=='Q') return false;
row--;
col--;
}
row=duprow;
col=dupcol;
//left
while(col>=0){
if(board[row][col]=='Q') return false;
col--;
}
row=duprow;
col=dupcol;
//lower diagonal
while(row<board.length && col>=0){
if(board[row][col]=='Q') return false;
row++;
col--;
}
return true;
}
}