|
| 1 | +/** |
| 2 | + * @param {number[][]} chessboard |
| 3 | + * @param {number[]} position |
| 4 | + * @return {number[][]} |
| 5 | + */ |
| 6 | +function getPossibleMoves(chessboard, position) { |
| 7 | + // Generate all knight moves (event those that goes beyond the board). |
| 8 | + const possibleMoves = [ |
| 9 | + [position[0] - 1, position[1] - 2], |
| 10 | + [position[0] - 2, position[1] - 1], |
| 11 | + [position[0] + 1, position[1] - 2], |
| 12 | + [position[0] + 2, position[1] - 1], |
| 13 | + [position[0] - 2, position[1] + 1], |
| 14 | + [position[0] - 1, position[1] + 2], |
| 15 | + [position[0] + 1, position[1] + 2], |
| 16 | + [position[0] + 2, position[1] + 1], |
| 17 | + ]; |
| 18 | + |
| 19 | + // Filter out all moves that go beyond the board. |
| 20 | + return possibleMoves.filter((move) => { |
| 21 | + const boardSize = chessboard.length; |
| 22 | + return move[0] >= 0 && move[1] >= 0 && move[0] < boardSize && move[1] < boardSize; |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {number[][]} chessboard |
| 28 | + * @param {number[]} move |
| 29 | + * @return {boolean} |
| 30 | + */ |
| 31 | +function isMoveAllowed(chessboard, move) { |
| 32 | + return chessboard[move[0]][move[1]] !== 1; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * @param {number[][]} chessboard |
| 37 | + * @param {number[][]} moves |
| 38 | + * @return {boolean} |
| 39 | + */ |
| 40 | +function isBoardCompletelyVisited(chessboard, moves) { |
| 41 | + const totalPossibleMovesCount = chessboard.length ** 2; |
| 42 | + const existingMovesCount = moves.length; |
| 43 | + |
| 44 | + return totalPossibleMovesCount === existingMovesCount; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * @param {number[][]} chessboard |
| 49 | + * @param {number[][]} moves |
| 50 | + * @return {boolean} |
| 51 | + */ |
| 52 | +function knightTourRecursive(chessboard, moves) { |
| 53 | + const currentChessboard = chessboard; |
| 54 | + |
| 55 | + // If board has been completely visited then we've found a solution. |
| 56 | + if (isBoardCompletelyVisited(currentChessboard, moves)) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + // Get next possible knight moves. |
| 61 | + const lastMove = moves[moves.length - 1]; |
| 62 | + const possibleMoves = getPossibleMoves(currentChessboard, lastMove); |
| 63 | + |
| 64 | + // Try to do next possible moves. |
| 65 | + for (let moveIndex = 0; moveIndex < possibleMoves.length; moveIndex += 1) { |
| 66 | + const currentMove = possibleMoves[moveIndex]; |
| 67 | + |
| 68 | + // Check if current move is allowed. We aren't allowed to go to |
| 69 | + // the same cells twice. |
| 70 | + if (isMoveAllowed(currentChessboard, currentMove)) { |
| 71 | + // Actually do the move. |
| 72 | + moves.push(currentMove); |
| 73 | + currentChessboard[currentMove[0]][currentMove[1]] = 1; |
| 74 | + |
| 75 | + // If further moves starting from current are successful then |
| 76 | + // return true meaning the solution is found. |
| 77 | + if (knightTourRecursive(currentChessboard, moves)) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + // BACKTRACKING. |
| 82 | + // If current move was unsuccessful then step back and try to do another move. |
| 83 | + moves.pop(); |
| 84 | + currentChessboard[currentMove[0]][currentMove[1]] = 0; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Return false if we haven't found solution. |
| 89 | + return false; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * @param {number} chessboardSize |
| 94 | + * @return {number[][]} |
| 95 | + */ |
| 96 | +export default function knightTour(chessboardSize) { |
| 97 | + // Init chessboard. |
| 98 | + const chessboard = Array(chessboardSize).fill(null).map(() => Array(chessboardSize).fill(0)); |
| 99 | + |
| 100 | + // Init moves array. |
| 101 | + const moves = []; |
| 102 | + |
| 103 | + // Do first move and place the knight to the 0x0 cell. |
| 104 | + const firstMove = [0, 0]; |
| 105 | + moves.push(firstMove); |
| 106 | + chessboard[firstMove[0]][firstMove[0]] = 1; |
| 107 | + |
| 108 | + // Recursively try to do the next move. |
| 109 | + const solutionWasFound = knightTourRecursive(chessboard, moves); |
| 110 | + |
| 111 | + return solutionWasFound ? moves : []; |
| 112 | +} |
0 commit comments