Start of the Backtracking Algorithm
We will start the backtracking process from row=0
and col=0
. col=0
part is already taken cared by the backtracking function itself. We just need to create a board so that our recursiveSolveNQueens
can start playing with the board.
To create a board, we will first create a string of size n
with the python string multiplication operator '.' * n
. Then we will again duplicate it n
times to get the board.
xxxxxxxxxx
11
const solveNQueens = (n) => {
// For n=5, board in Javascript is
// [".....", => '.'*n
// ".....", => '.'*n
// ".....", => '.'*n
// ".....", => '.'*n
// "....."] => '.'*n
// so, [...Array(n)].map(_ => '.'.repeat(n))
let board = [Array(n)].map(_ => '.'.repeat(n));
// Type will be replaced by actual type later
};
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment