|
1 |
| -export default function (n) { |
2 |
| - // Keeps track of the # of valid solutions |
3 |
| - let count = 0; |
4 |
| - |
5 |
| - // Helps identify valid solutions |
6 |
| - const done = (2 ** n) - 1; |
7 |
| - |
8 |
| - // Checks all possible board configurations |
9 |
| - const innerRecurse = (ld, col, rd) => { |
10 |
| - // All columns are occupied, |
11 |
| - // so the solution must be complete |
12 |
| - if (col === done) { |
13 |
| - count += 1; |
14 |
| - return; |
15 |
| - } |
16 |
| - |
17 |
| - // Gets a bit sequence with "1"s |
18 |
| - // whereever there is an open "slot" |
19 |
| - let poss = ~(ld | rd | col); |
20 |
| - |
21 |
| - // Loops as long as there is a valid |
22 |
| - // place to put another queen. |
23 |
| - while (poss & done) { |
24 |
| - const bit = poss & -poss; |
25 |
| - poss -= bit; |
26 |
| - innerRecurse((ld | bit) >> 1, col | bit, (rd | bit) << 1); |
27 |
| - } |
28 |
| - }; |
29 |
| - |
30 |
| - innerRecurse(0, 0, 0); |
31 |
| - |
32 |
| - return count; |
| 1 | +/** |
| 2 | + * Checks all possible board configurations. |
| 3 | + * |
| 4 | + * @param {number} boardSize - Size of the squared chess board. |
| 5 | + * @param {number} leftDiagonal - Sequence of N bits that show whether the corresponding location |
| 6 | + * on the current row is "available" (no other queens are threatening from left diagonal). |
| 7 | + * @param {number} column - Sequence of N bits that show whether the corresponding location |
| 8 | + * on the current row is "available" (no other queens are threatening from columns). |
| 9 | + * @param {number} rightDiagonal - Sequence of N bits that show whether the corresponding location |
| 10 | + * on the current row is "available" (no other queens are threatening from right diagonal). |
| 11 | + * @param {number} solutionsCount - Keeps track of the number of valid solutions. |
| 12 | + * @return {number} - Number of possible solutions. |
| 13 | + */ |
| 14 | +function nQueensBitwiseRecursive( |
| 15 | + boardSize, |
| 16 | + leftDiagonal = 0, |
| 17 | + column = 0, |
| 18 | + rightDiagonal = 0, |
| 19 | + solutionsCount = 0, |
| 20 | +) { |
| 21 | + // Keeps track of the number of valid solutions. |
| 22 | + let currentSolutionsCount = solutionsCount; |
| 23 | + |
| 24 | + // Helps to identify valid solutions. |
| 25 | + // isDone simply has a bit sequence with 1 for every entry up to the Nth. For example, |
| 26 | + // when N=5, done will equal 11111. The "isDone" variable simply allows us to not worry about any |
| 27 | + // bits beyond the Nth. |
| 28 | + const isDone = (2 ** boardSize) - 1; |
| 29 | + |
| 30 | + // All columns are occupied (i.e. 0b1111 for boardSize = 4), so the solution must be complete. |
| 31 | + // Since the algorithm never places a queen illegally (ie. when it can attack or be attacked), |
| 32 | + // we know that if all the columns have been filled, we must have a valid solution. |
| 33 | + if (column === isDone) { |
| 34 | + return currentSolutionsCount + 1; |
| 35 | + } |
| 36 | + |
| 37 | + // Gets a bit sequence with "1"s wherever there is an open "slot". |
| 38 | + // All that's happening here is we're taking col, ld, and rd, and if any of the columns are |
| 39 | + // "under attack", we mark that column as 0 in poss, basically meaning "we can't put a queen in |
| 40 | + // this column". Thus all bits position in poss that are '1's are available for placing |
| 41 | + // queen there. |
| 42 | + let availablePositions = ~(leftDiagonal | rightDiagonal | column); |
| 43 | + |
| 44 | + // Loops as long as there is a valid place to put another queen. |
| 45 | + // For N=4 the isDone=0b1111. Then if availablePositions=0b0000 (which would mean that all places |
| 46 | + // are under threatening) we must stop trying to place a queen. |
| 47 | + while (availablePositions & isDone) { |
| 48 | + // firstAvailablePosition just stores the first non-zero bit (ie. the first available location). |
| 49 | + // So if firstAvailablePosition was 0010, it would mean the 3rd column of the current row. |
| 50 | + // And that would be the position will be placing our next queen. |
| 51 | + // |
| 52 | + // For example: |
| 53 | + // availablePositions = 0b01100 |
| 54 | + // firstAvailablePosition = 100 |
| 55 | + const firstAvailablePosition = availablePositions & -availablePositions; |
| 56 | + |
| 57 | + // This line just marks that position in the current row as being "taken" by flipping that |
| 58 | + // column in availablePositions to zero. This way, when the while loop continues, we'll know |
| 59 | + // not to try that location again. |
| 60 | + // |
| 61 | + // For example: |
| 62 | + // availablePositions = 0b0100 |
| 63 | + // firstAvailablePosition = 0b10 |
| 64 | + // 0b0110 - 0b10 = 0b0100 |
| 65 | + availablePositions -= firstAvailablePosition; |
| 66 | + |
| 67 | + /* |
| 68 | + * The operators >> 1 and 1 << simply move all the bits in a bit sequence one digit to the |
| 69 | + * right or left, respectively. So calling (rd|bit)<<1 simply says: combine rd and bit with |
| 70 | + * an OR operation, then move everything in the result to the left by one digit. |
| 71 | + * |
| 72 | + * More specifically, if rd is 0001 (meaning that the top-right-to-bottom-left diagonal through |
| 73 | + * column 4 of the current row is occupied), and bit is 0100 (meaning that we are planning to |
| 74 | + * place a queen in column 2 of the current row), (rd|bit) results in 0101 (meaning that after |
| 75 | + * we place a queen in column 2 of the current row, the second and the fourth |
| 76 | + * top-right-to-bottom-left diagonals will be occupied). |
| 77 | + * |
| 78 | + * Now, if add in the << operator, we get (rd|bit)<<1, which takes the 0101 we worked out in |
| 79 | + * our previous bullet point, and moves everything to the left by one. The result, therefore, |
| 80 | + * is 1010. |
| 81 | + */ |
| 82 | + currentSolutionsCount += nQueensBitwiseRecursive( |
| 83 | + boardSize, |
| 84 | + (leftDiagonal | firstAvailablePosition) >> 1, |
| 85 | + column | firstAvailablePosition, |
| 86 | + (rightDiagonal | firstAvailablePosition) << 1, |
| 87 | + solutionsCount, |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + return currentSolutionsCount; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * @param {number} boardSize - Size of the squared chess board. |
| 96 | + * @return {number} - Number of possible solutions. |
| 97 | + * @see http://gregtrowbridge.com/a-bitwise-solution-to-the-n-queens-problem-in-javascript/ |
| 98 | + */ |
| 99 | +export default function nQueensBitwise(boardSize) { |
| 100 | + return nQueensBitwiseRecursive(boardSize); |
33 | 101 | }
|
0 commit comments