Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2313e29

Browse files
Create 51. N-Queens.java
1 parent a0136f8 commit 2313e29

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

‎Leetcode/51. N-Queens.java‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
class Solution {
2+
public List<List<String>> solveNQueens(int n) {
3+
// TC - N^N
4+
List<List<String>> allBoards = new ArrayList<>(); // list of all possible solution i.e output
5+
// to avoid complexity instead of creating list of string
6+
// we will make a 2D character array for ease
7+
char[][] board = new char[n][n];
8+
helper(allBoards, board, 0); // function to check a single board
9+
return allBoards; // output result
10+
}
11+
12+
public void helper(List<List<String>> allBoards, char[][] board, int col){
13+
// base case
14+
// if the current board is valid save it and try next configuration
15+
if(col == board.length){
16+
saveBoard(allBoards, board); // to save the current board in the allBoards list to send output of all the possible solutions to place queens
17+
return;
18+
}
19+
20+
// to go to each cell and put or remove queen according to the validity
21+
for(int row = 0; row < board.length; row++){
22+
if(isSafe(row, col, board)){ // isSafe helper function to check validity at current point
23+
board[row][col] = 'Q';
24+
helper(allBoards, board, col + 1);
25+
// if the current place of queen for to be wrong after checking further cells
26+
// remove queen from that position
27+
board[row][col] = '.';
28+
}
29+
}
30+
}
31+
32+
public boolean isSafe(int row, int col, char[][] board){
33+
// check all directions
34+
35+
// FOR ROW
36+
for(int x = 0; x < board.length; x++){
37+
if(board[row][x] == 'Q'){
38+
return false;
39+
}
40+
41+
}
42+
43+
44+
// FOR COLUMN
45+
for(int y = 0; y < board.length; y++){
46+
if(board[y][col] == 'Q'){
47+
return false;
48+
}
49+
50+
}
51+
52+
// FOR DIAGNOLS
53+
54+
// upper left (r-1, c-1)
55+
int r = row; // original position
56+
for(int c = col; c >= 0 && r >= 0; c--, r--){
57+
if(board[r][c] == 'Q'){
58+
return false;
59+
}
60+
}
61+
62+
// upper right (r-1, c+1)
63+
r = row;
64+
for(int c = col; c < board.length && r >=0; c++, r--){
65+
if(board[r][c] == 'Q'){
66+
return false;
67+
}
68+
}
69+
70+
// lower left (r+1, c-1)
71+
r = row;
72+
for(int c = col; c >= 0 && r < board.length; r++, c--){
73+
if(board[r][c] == 'Q'){
74+
return false;
75+
}
76+
}
77+
78+
// lower right (r+1, c+1)
79+
r = row;
80+
for(int c = col; c < board.length && r < board.length; r++, c++){
81+
if(board[r][c] == 'Q'){
82+
return false;
83+
}
84+
}
85+
86+
return true;
87+
}
88+
89+
public void saveBoard(List<List<String>> allBoards, char[][] board){
90+
String row = "";
91+
List<String> FinalBoard = new ArrayList<>();
92+
93+
for(int i = 0; i < board.length; i++){
94+
row = ""; // empty string
95+
for(int j = 0; j < board[0].length; j++){
96+
if(board[i][j] == 'Q'){
97+
row += 'Q';
98+
} else{
99+
row += '.';
100+
}
101+
}
102+
FinalBoard.add(row);
103+
}
104+
105+
allBoards.add(FinalBoard);
106+
}
107+
}
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /