forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
N-Queens.js
203 lines (162 loc) · 5.23 KB
/
N-Queens.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* The n-queens puzzle is the problem of placing n queens
on an n x n chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle. */
/* Function to get the number of way N queens can be placed on a NxN chess board */
const totalNQueens = (n) => {
/* Helper Function */
const getTotalWays = (board, row, col, n) => {
/* Base case when we filled each and every row */
if(row == n){
return true;
}
/* Checking every column in a particular row */
for(let i=col; i<n; i++){
/* Checking if a queen is already placed in that particular spot */
if(!board[row][i]){
/* Placing a queen in that spot */
board[row][i] = true;
/* Checking the validity of our move and continuing to place queens in the next row */
if(validMove(board, row, i, n) && getTotalWays(board, row+1, 0, n)){
/* Incrementing our answer if n queens are placed on the chessboard */
ans++;
}
/* Removing the queen in that spot to get a different combination */
board[row][i] = false;
}
}
/* Returning false if we cannot place any queens in that row */
return false;
}
/* Function to check the validity of our move */
const validMove = (board, row, col, n) => {
return validCol(board, row, col, n) &&
validRow(board, row, col, n) &&
validRightUpperDiagonal(board, row, col, n) &&
validRightLowerDiagonal(board, row, col, n) &&
validLeftUpperDiagonal(board, row, col) &&
validLeftLowerDiagonal(board, row, col, n)
}
/* Function to check if no two queens can kill each other in their respective columns */
const validCol = (board, row, col, n) => {
let count = 0;
for(let i=0; i<n; i++){
if(board[i][col]){
count++;
}
if(count > 1){
return false;
}
}
return true;
}
/* Function to check if no two queens can kill each other in their respective row */
const validRow = (board, row, col, n) => {
let count = 0;
for(let i=0; i<n; i++){
if(board[row][i]){
count++;
}
if(count > 1){
return false;
}
}
return true;
}
/* Function to check if no two queens can kill each other in their respective right upper diagonal */
const validRightUpperDiagonal = (board, row, col, n) => {
let r = row-1;
let c = col+1;
let count = 0;
while(r>=0 && c<n){
if(board[r][c]){
count++;
}
if(count > 0){
return false;
}
r--;
c++;
}
return true;
}
/* Function to check if no two queens can kill each other in their respective right lower diagonal */
const validRightLowerDiagonal = (board, row, col, n) => {
let r = row+1;
let c = col+1;
let count = 0;
while(r<n && c<n){
if(board[r][c]){
count++;
}
if(count > 0){
return false;
}
r++;
c++;
}
return true;
}
/* Function to check if no two queens can kill each other in their respective left upper diagonal */
const validLeftUpperDiagonal = (board, row, col) => {
let r = row-1;
let c = col-1;
let count = 0;
while(r>=0 && c>=0){
if(board[r][c]){
count++;
}
if(count > 0){
return false;
}
r--;
c--;
}
return true;
}
/* Function to check if no two queens can kill each other in their respective left lower diagonal */
const validLeftLowerDiagonal = (board, row, col, n) => {
let r = row+1;
let c = col-1;
let count = 0;
while(r<n && c>=0){
if(board[r][c]){
count++;
}
if(count > 0){
return false;
}
r++;
c--;
}
return true;
}
const createRow = ()=>{
return new Array(n).fill(false);
}
/* Creating an empty board */
let board = new Array(n).fill(null).map(createRow);
let ans = 0;
/* Gets the total number of way N Queens can be placed */
getTotalWays(board, 0, 0, n);
/* Returning the answer */
return ans;
};
/* Printing the answer for 10x10 chessboard -> Ans = 724 ways */
console.log(`${totalNQueens(10)} ways`);
/*Example 1
Input :
4
Output :
2
Explanation :
There are only two possible ways to place 4 Queens on a 4x4 chessboard
Example 2
Input :
10
Output :
724
Explanation :
There are 724 possible ways to place 10 Queens on a 10x10 chessboard
Time Complexity : O(N^N)
Space Complexity : O(NxN)
*/