|
1 |
| -# N Queens Problem |
2 |
| -The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. |
| 1 | +# N-Queens Problem |
3 | 2 |
|
4 |
| -## Applications |
5 |
| -* Puzzle Solving |
6 |
| -* Searching |
| 3 | +The **eight queens puzzle** is the problem of placing eight chess queens |
| 4 | +on an `8×8` chessboard so that no two queens threaten each other. |
| 5 | +Thus, a solution requires that no two queens share the same row, |
| 6 | +column, or diagonal. The eight queens puzzle is an example of the |
| 7 | +more general *n queens problem* of placing n non-attacking queens |
| 8 | +on an `n×n` chessboard, for which solutions exist for all natural |
| 9 | +numbers `n` with the exception of `n=2` and `n=3`. |
7 | 10 |
|
8 |
| -## Complexity |
9 |
| -* **Time**: Worst ) |
10 |
| -* **Space**: Worst ) |
| 11 | +For example, following is a solution for 4 Queen problem. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +The expected output is a binary matrix which has 1s for the blocks |
| 16 | +where queens are placed. For example following is the output matrix |
| 17 | +for above 4 queen solution. |
| 18 | + |
| 19 | +``` |
| 20 | +{ 0, 1, 0, 0} |
| 21 | +{ 0, 0, 0, 1} |
| 22 | +{ 1, 0, 0, 0} |
| 23 | +{ 0, 0, 1, 0} |
| 24 | +``` |
| 25 | + |
| 26 | +## Naive Algorithm |
| 27 | + |
| 28 | +Generate all possible configurations of queens on board and print a |
| 29 | +configuration that satisfies the given constraints. |
| 30 | + |
| 31 | +``` |
| 32 | +while there are untried configurations |
| 33 | +{ |
| 34 | + generate the next configuration |
| 35 | + if queens don't attack in this configuration then |
| 36 | + { |
| 37 | + print this configuration; |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## Backtracking Algorithm |
| 43 | + |
| 44 | +The idea is to place queens one by one in different columns, |
| 45 | +starting from the leftmost column. When we place a queen in a |
| 46 | +column, we check for clashes with already placed queens. In |
| 47 | +the current column, if we find a row for which there is no |
| 48 | +clash, we mark this row and column as part of the solution. |
| 49 | +If we do not find such a row due to clashes then we backtrack |
| 50 | +and return false. |
| 51 | + |
| 52 | +``` |
| 53 | +1) Start in the leftmost column |
| 54 | +2) If all queens are placed |
| 55 | + return true |
| 56 | +3) Try all rows in the current column. Do following for every tried row. |
| 57 | + a) If the queen can be placed safely in this row then mark this [row, |
| 58 | + column] as part of the solution and recursively check if placing |
| 59 | + queen here leads to a solution. |
| 60 | + b) If placing queen in [row, column] leads to a solution then return |
| 61 | + true. |
| 62 | + c) If placing queen doesn't lead to a solution then umark this [row, |
| 63 | + column] (Backtrack) and go to step (a) to try other rows. |
| 64 | +3) If all rows have been tried and nothing worked, return false to trigger |
| 65 | + backtracking. |
| 66 | +``` |
| 67 | + |
| 68 | +## Bitwise Solution |
| 69 | + |
| 70 | +Bitwise algorithm basically approaches the problem like this: |
| 71 | + |
| 72 | +- Queens can attack diagonally, vertically, or horizontally. As a result, there |
| 73 | +can only be one queen in each row, one in each column, and at most one on each |
| 74 | +diagonal. |
| 75 | +- Since we know there can only one queen per row, we will start at the first row, |
| 76 | +place a queen, then move to the second row, place a second queen, and so on until |
| 77 | +either a) we reach a valid solution or b) we reach a dead end (ie. we can't place |
| 78 | +a queen such that it is "safe" from the other queens). |
| 79 | +- Since we are only placing one queen per row, we don't need to worry about |
| 80 | +horizontal attacks, since no queen will ever be on the same row as another queen. |
| 81 | +- That means we only need to check three things before placing a queen on a |
| 82 | +certain square: 1) The square's column doesn't have any other queens on it, 2) |
| 83 | +the square's left diagonal doesn't have any other queens on it, and 3) the |
| 84 | +square's right diagonal doesn't have any other queens on it. |
| 85 | +- If we ever reach a point where there is nowhere safe to place a queen, we can |
| 86 | +give up on our current attempt and immediately test out the next possibility. |
| 87 | + |
| 88 | +First let's talk about the recursive function. You'll notice that it accepts |
| 89 | +3 parameters: `leftDiagonal`, `column`, and `rightDiagonal`. Each of these is |
| 90 | +technically an integer, but the algorithm takes advantage of the fact that an |
| 91 | +integer is represented by a sequence of bits. So, think of each of these |
| 92 | +parameters as a sequence of `N` bits. |
| 93 | + |
| 94 | +Each bit in each of the parameters represents whether the corresponding location |
| 95 | +on the current row is "available". |
| 96 | + |
| 97 | +For example: |
| 98 | +- For `N=4`, column having a value of `0010` would mean that the 3rd column is |
| 99 | +already occupied by a queen. |
| 100 | +- For `N=8`, ld having a value of `00011000` at row 5 would mean that the |
| 101 | +top-left-to-bottom-right diagonals that pass through columns 4 and 5 of that |
| 102 | +row are already occupied by queens. |
| 103 | + |
| 104 | +Below is a visual aid for `leftDiagonal`, `column`, and `rightDiagonal`. |
| 105 | + |
| 106 | + |
11 | 107 |
|
12 | 108 | ## References
|
13 |
| -* [geeksforgeeks](http://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/) |
| 109 | + |
| 110 | +- [trekhleb/javascript-algorithms](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/uncategorized/n-queens) |
| 111 | +- [Wikipedia](https://en.wikipedia.org/wiki/Eight_queens_puzzle) |
| 112 | +- [GeeksForGeeks](https://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/) |
| 113 | +- [On YouTube by Abdul Bari](https://www.youtube.com/watch?v=xFv_Hl4B83A&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8) |
| 114 | +- [On YouTube by Tushar Roy](https://www.youtube.com/watch?v=xouin83ebxE&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8) |
| 115 | +- Bitwise Solution |
| 116 | + - [Wikipedia](https://en.wikipedia.org/wiki/Eight_queens_puzzle) |
| 117 | + - [Solution by Greg Trowbridge](http://gregtrowbridge.com/a-bitwise-solution-to-the-n-queens-problem-in-javascript/) |
0 commit comments