Search code examples
javascriptalgorithmn-queens

Finding Multiple Solutions to "8 Queens"


Some of you computer science, mathematic, etc... .majors might have experience with this problem. It is famously known as the 8 Queens. Essentially, how many different ways can you place 8 queens on an 8x8 chess board so that none of them conflict (aka diagonally or horizontally). I attempted this problem below, but my program only prints out a single solution.

I imagine I need a counter. I am not sure how to continue, and do not have much of a background in algorithms. Any assistance is greatly appreciated, thank you for spending your time to help.

var n = 8;

solveNQ();

function printSolution(board){
  for(var i=0; i<n; i++){
    for(var j=0; j<n; j++){
      document.write(" "+board[i][j]+" ");
    }
    document.write("<br>");
  }
  document.write("<br>");
}

function isSafe(board, row, col){

  // Checks the ← direction
  for(var i=0; i<col; i++){
    if (board[row][i] === 1) {
      return false;
    }
  }

  // Checks the ↖ direction 
  for(var i=row, j=col; i>=0 && j>=0; i--, j--){
    if (board[i][j] === 1) {
      return false;
    }
  }

  // Checks the ↙ direction 
  for(var i=row, j=col; j>=0 && i<n; i++, j--){
    if (board[i][j] === 1){
      return false;
    }
  }

  return true;
}


function recurseNQ(board, col){
  if(col>=n){
    return true;
  }

  for(var i=0; i<n; i++){
    if(isSafe(board, i, col)){
      board[i][col]=1;

      if(recurseNQ(board, col+1)===true)
        return true;

      board[i][col]=0;
    }
  }
  return false;
}


function solveNQ(){
  var board = generateBoard(n);
  if(recurseNQ(board, 0)===false){
    console.log("No solution found");
    return false;
  }
  printSolution(board);
}

function generateBoard(n){
  var board=[];
  for(var i=0; i<n; i++){
    board[i]=[];
    for(var j=0; j<n; j++){
      board[i][j]=0;
    }
  }
  return board;
}

Solution

  • You just don't have to return from recurseNQ when solution found. Also print solution each time col equals to 8. Code with slight modifications below. The code produces 92 valid solutions which should be the case

    var n = 8;
    
    solveNQ();
    
    function printSolution(board){
      for(var i=0; i<n; i++){
        for(var j=0; j<n; j++){
          document.write(" "+board[i][j]+" ");
        }
        document.write("<br>");
      }
      document.write("<br>");
    }
    
    function isSafe(board, row, col){
    
      // Checks the ← direction
      for(var i=0; i<col; i++){
        if (board[row][i] === 1) {
          return false;
        }
      }
    
      // Checks the ↖ direction 
      for(var i=row, j=col; i>=0 && j>=0; i--, j--){
        if (board[i][j] === 1) {
          return false;
        }
      }
    
      // Checks the ↙ direction 
      for(var i=row, j=col; j>=0 && i<n; i++, j--){
        if (board[i][j] === 1){
          return false;
        }
      }
    
      return true;
    }
    
    
    function recurseNQ(board, col){
      if(col===n){
          printSolution(board); // <-- print another solution when n==8
        return;  
      }
    
      for(var i=0; i<n; i++){
        if(isSafe(board, i, col)){
          board[i][col]=1;
    
          recurseNQ(board, col+1);
          //if(recurseNQ(board, col+1)===true) //<-- you don't need this
              // return true;
    
          board[i][col]=0;
        }
      }
      return false;
    }
    
    
    function solveNQ(){
      var board = generateBoard(n);
      recurseNQ(board, 0);
      //if(recurseNQ(board, 0)===false){
        //console.log("No solution found");
       // return false;
     // }
     // printSolution(board);
    }
    
    function generateBoard(n){
      var board=[];
      for(var i=0; i<n; i++){
        board[i]=[];
        for(var j=0; j<n; j++){
          board[i][j]=0;
        }
      }
      return board;
    }