Skip to content

Commit

Permalink
Add day17
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 11, 2019
1 parent 172b125 commit f630bb8
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
14. [Day 14 -- Sum of digits and product of numbers](./day14) -- [http://codetoexpress.tech/dc/day14/](http://codetoexpress.tech/dc/day14/)
15. [Day 15 -- Pascal's Triangle](./day15) -- [http://codetoexpress.tech/dc/day15/](http://codetoexpress.tech/dc/day15/)
16. [Day 16 -- Tower of Hanoi](./day16) -- [http://codetoexpress.tech/dc/day16/](http://codetoexpress.tech/dc/day16/)
17. [Day 17 -- N Queens Problem](./day17) -- [http://codetoexpress.tech/dc/day17/](http://codetoexpress.tech/dc/day17/)

## Timeline

Expand Down
142 changes: 142 additions & 0 deletions day17/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
![cover](./cover.png)

# Day 17

Today's Problem - N Queens Problem

**Question** -- Write a program to find out placement of N queens on an NxN chessboard such that none of them can attack each other

Example

```
input: 4
output:
* * Q *
Q * * *
* * * Q
* Q * *
```

Note that the output can be printed in the form of a binary NxN matrix with 1 representing the queen's position

Example

```
{ 0, 1, 0, 0}
{ 0, 0, 0, 1}
{ 1, 0, 0, 0}
{ 0, 0, 1, 0}
```

![ques](./ques.png)

### Method - Backtracking

Source: [Geeks4Geeks](https://www.geeksforgeeks.org/n-queen-problem-backtracking-3/)

Method

```
1) Start in the leftmost column
2) If all queens are placed
return true
3) Try all rows in the current column. Do following for every tried row.
a) If the queen can be placed safely in this row then mark this [row,
column] as part of the solution and recursively check if placing queen here leads to a solution.
b) If placing the queen in [row, column] leads to a solution then return
true.
c) If placing queen doesn't lead to a solution then umark this [row,
column] (Backtrack) and go to step (a) to try other rows.
3) If all rows have been tried and nothing worked, return false to trigger
backtracking.
```

## JavaScript Implementation

### [Solution](./JavaScript/nqueens.js)

```js
/**
* @date 11/01/2018
* Method (Backtracking) taken from -- https://www.youtube.com/watch?v=0DeznFqrgAI
* Implemented in JavaScript by @MadhavBahlMD
*/

function nqueens (num) {
console.log (`Solving ${num} Queens Problem`);

// Initialize chessBoard
let chessBoard = [];
for (let i=0; i<num; i++) {
let thisRow = [];
for (let j=0; j<num; j++)
thisRow.push (0);
chessBoard.push(thisRow);
}

// Check whether solution exists or not
if (!solveNqueens(num, chessBoard, 0)) {
console.log ('No combinations');
return 0;
}

printBoard (chessBoard);
return 1;
}

function solveNqueens (num, chessBoard, col) {
// If all queens are placed, return true
// We find all queens are place when the value of current column (col) becomes greater than or equals to N (num)
if (col >= num)
return true;

// Place all queen in all rows one by one and check whether they can be placed or not
for (let i=0; i<num; i++) {
// Check if the queen can be placed in ith row
if (canBePlaced (num, chessBoard, i, col)) {
chessBoard [i][col] = 1;

// Use recursion to place rest of the queens
if (solveNqueens (num, chessBoard, col+1))
return true;

// If current queen placement doesnt lead to a solution, remove the queen
chessBoard [i][col] = 0;
}
}

return false;
}

function canBePlaced (num, chessBoard, row, col) {
// Check row on left side
for (let i=0; i<col; i++)
if (chessBoard[row][i] === 1)
return false;

// Check diagonals
for (let i=row, j=col; i>=0 && j>=0; i--, j--)
if (chessBoard[i][j] === 1)
return false;

for (let i=row, j=col; j>=0 && i<num; i++, j--)
if (chessBoard[i][j] === 1)
return false;

// Return true
return true;
}

function printBoard (chessBoard) {
let outputString;

for (let row of chessBoard) {
outputString = '';
for (let element of row)
outputString += element + ' ';
console.log (outputString);
}
}

nqueens (8);
```
Binary file added day17/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day17/ques.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f630bb8

Please sign in to comment.