I want to reporduce the placement on the board while the backtracking algorithm work. If I insert a Timeout, the ram of the tab fastly increase and crashes. How can i show the functionlity of the algorithm ?
function solveNQUtil(board, col) {
/* base case: If all queens are placed
then return true */
if (col >= N)
return true;
/* Consider this column and try placing
this queen in all rows one by one */
for (let i = 0; i < N; i++) {
/* Check if the queen can be placed on
board[i][col] */
place(i,col);
setTimeout(function (){
if (isSafe(board, i, col)) {
/* Place this queen in board[i][col] */
board[i][col] = 1;
/* recur to place rest of the queens */
if (solveNQUtil(board, col + 1) == true) {
return true;
}
/* If placing queen in board[i][col]
doesn't lead to a solution then
remove queen from board[i][col] */
remove(i,col);
board[i][col] = 0; // BACKTRACK
}
else{
remove(i,col);
}
}, 1000);
}
/* If the queen can not be placed in any row in
this colum col, then return false */
return false;
}
function place(i,col){
let id="chest"+i+""+col;
document.getElementById(id).innerHTML ="♕"
}
function remove(i,col){
let id="chest"+i+""+col;
document.getElementById(id).innerHTML=""
}
The function place and remove show the queens on the table at that position( AND they works fine) If i remove timeOut, i see only the final solution but not the resolution;