Skip to content

Commit f630bb8

Browse files
committed
Add day17
1 parent 172b125 commit f630bb8

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
3333
14. [Day 14 -- Sum of digits and product of numbers](./day14) -- [http://codetoexpress.tech/dc/day14/](http://codetoexpress.tech/dc/day14/)
3434
15. [Day 15 -- Pascal's Triangle](./day15) -- [http://codetoexpress.tech/dc/day15/](http://codetoexpress.tech/dc/day15/)
3535
16. [Day 16 -- Tower of Hanoi](./day16) -- [http://codetoexpress.tech/dc/day16/](http://codetoexpress.tech/dc/day16/)
36+
17. [Day 17 -- N Queens Problem](./day17) -- [http://codetoexpress.tech/dc/day17/](http://codetoexpress.tech/dc/day17/)
3637

3738
## Timeline
3839

day17/README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
![cover](./cover.png)
2+
3+
# Day 17
4+
5+
Today's Problem - N Queens Problem
6+
7+
**Question** -- Write a program to find out placement of N queens on an NxN chessboard such that none of them can attack each other
8+
9+
Example
10+
11+
```
12+
input: 4
13+
output:
14+
* * Q *
15+
Q * * *
16+
* * * Q
17+
* Q * *
18+
```
19+
20+
Note that the output can be printed in the form of a binary NxN matrix with 1 representing the queen's position
21+
22+
Example
23+
24+
```
25+
{ 0, 1, 0, 0}
26+
{ 0, 0, 0, 1}
27+
{ 1, 0, 0, 0}
28+
{ 0, 0, 1, 0}
29+
```
30+
31+
![ques](./ques.png)
32+
33+
### Method - Backtracking
34+
35+
Source: [Geeks4Geeks](https://www.geeksforgeeks.org/n-queen-problem-backtracking-3/)
36+
37+
Method
38+
39+
```
40+
1) Start in the leftmost column
41+
2) If all queens are placed
42+
return true
43+
3) Try all rows in the current column. Do following for every tried row.
44+
a) If the queen can be placed safely in this row then mark this [row,
45+
column] as part of the solution and recursively check if placing queen here leads to a solution.
46+
b) If placing the queen in [row, column] leads to a solution then return
47+
true.
48+
c) If placing queen doesn't lead to a solution then umark this [row,
49+
column] (Backtrack) and go to step (a) to try other rows.
50+
3) If all rows have been tried and nothing worked, return false to trigger
51+
backtracking.
52+
```
53+
54+
## JavaScript Implementation
55+
56+
### [Solution](./JavaScript/nqueens.js)
57+
58+
```js
59+
/**
60+
* @date 11/01/2018
61+
* Method (Backtracking) taken from -- https://www.youtube.com/watch?v=0DeznFqrgAI
62+
* Implemented in JavaScript by @MadhavBahlMD
63+
*/
64+
65+
function nqueens (num) {
66+
console.log (`Solving ${num} Queens Problem`);
67+
68+
// Initialize chessBoard
69+
let chessBoard = [];
70+
for (let i=0; i<num; i++) {
71+
let thisRow = [];
72+
for (let j=0; j<num; j++)
73+
thisRow.push (0);
74+
chessBoard.push(thisRow);
75+
}
76+
77+
// Check whether solution exists or not
78+
if (!solveNqueens(num, chessBoard, 0)) {
79+
console.log ('No combinations');
80+
return 0;
81+
}
82+
83+
printBoard (chessBoard);
84+
return 1;
85+
}
86+
87+
function solveNqueens (num, chessBoard, col) {
88+
// If all queens are placed, return true
89+
// We find all queens are place when the value of current column (col) becomes greater than or equals to N (num)
90+
if (col >= num)
91+
return true;
92+
93+
// Place all queen in all rows one by one and check whether they can be placed or not
94+
for (let i=0; i<num; i++) {
95+
// Check if the queen can be placed in ith row
96+
if (canBePlaced (num, chessBoard, i, col)) {
97+
chessBoard [i][col] = 1;
98+
99+
// Use recursion to place rest of the queens
100+
if (solveNqueens (num, chessBoard, col+1))
101+
return true;
102+
103+
// If current queen placement doesnt lead to a solution, remove the queen
104+
chessBoard [i][col] = 0;
105+
}
106+
}
107+
108+
return false;
109+
}
110+
111+
function canBePlaced (num, chessBoard, row, col) {
112+
// Check row on left side
113+
for (let i=0; i<col; i++)
114+
if (chessBoard[row][i] === 1)
115+
return false;
116+
117+
// Check diagonals
118+
for (let i=row, j=col; i>=0 && j>=0; i--, j--)
119+
if (chessBoard[i][j] === 1)
120+
return false;
121+
122+
for (let i=row, j=col; j>=0 && i<num; i++, j--)
123+
if (chessBoard[i][j] === 1)
124+
return false;
125+
126+
// Return true
127+
return true;
128+
}
129+
130+
function printBoard (chessBoard) {
131+
let outputString;
132+
133+
for (let row of chessBoard) {
134+
outputString = '';
135+
for (let element of row)
136+
outputString += element + ' ';
137+
console.log (outputString);
138+
}
139+
}
140+
141+
nqueens (8);
142+
```

day17/cover.png

141 KB
Loading

day17/ques.png

507 KB
Loading

0 commit comments

Comments
 (0)