I have coded N-Queen problem using backtracking in javascript.
what I want to do?
I want to spot execution of script for a second every time it reaches to solution and show the solution using animation.
what is the problem?
code execution is so fast that i can not see any color transition.
here is a part of my code (where i want to spot execution of script for a second). for full code please refer to http://jsfiddle.net/a603smud/1/
function placeQueen(row){
for(var i=0;i<N && row<N;i++){
chess[row][i] = 1;
var temp = row*N+i+1;
//place a Queen (red color)
$('#'+temp).css("background-color","red");
//check if place is safe
if(check(row,i)){
if(row==N-1){
//place is safe and it is last row then
//solution found
//stop execution for a second
//then continue
print();
}
else{
setTimeout(function(){
placeQueen(row+1);
},1000);
}
}
//remove the Queen (backtracking)
$('#'+temp).css("background-color","blue");
chess[row][i]=0;
}
}
Red color : queen is placed on the box blue color : box is empty
Any help would be appreciated.