|
| 1 | + |
| 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 | + |
| 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 | +``` |
0 commit comments