Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with java project. Project 8 Queens Problem Definition: The eight qu

ID: 3793581 • Letter: P

Question

Please help with java project.

Project 8 Queens Problem Definition: The eight queens puzzle is based on the classic strategy games problem which is in this case putting eight chess queens on an 8 by 8 chessboard such that none of them is able to capture any other using the standard chess queen's moves. The color of the queens is meaningless in this puzzle, and any queen is assumed to be able to attack any other. Thus, a solution requires that no two queens share the same row, column, or diagonal.

Explanation / Answer

Answer:

Answer:

import java.util.*;


public class EightQueens {

   private int[] perm;
   private boolean[] used;
   private int numsols;
  
   public static void main(String[] args) {
   EightQueens obj = new EightQueens(5);
   obj.solveIt();
   obj.printNumSols();
   }
  

   public EightQueens(int n) {
      
       perm = new int[n];
       used = new boolean[n];
       numsols = 0;
      
       for (int i=0; i<n; i++) {
           perm[i] = -1;
           used[i] = false;
       }
   }

  
   public void solveIt() {
  

    solveItRec(0);
   }

  
   public void solveItRec(int location) {

   int i;
  

   if (location == perm.length) {
   printSol();
   numsols++;
   }
  
       for (i=0; i<perm.length; i++) {
  

   if (used[i] == false) {
  
  
   if (!conflict(location, i)) {
  
  
   perm[location] = i;
  
       used[i] = true;
  
  
   solveItRec(location+1);
  
  
   used[i] = false;
   }   

   }
   }

   }

   private boolean conflict(int location, int row) {
  
   int i;
  
       for (i=0; i<location; i++)
  
  
   if (Math.abs(location - i) == Math.abs(perm[i] - row))
   return true;
  
  
   return false;
   }

  
   public void printSol() {

   int i,j;
  
    System.out.println("Here is a solution: ");

   
    for (i=0; i<perm.length; i++) {

   
    for (j=0; j<perm.length; j++) {


    if (perm[j] == i)   
   System.out.print("Q ");

   
    else
   System.out.print("_ ");
    }
    System.out.println(" ");
    }
   }
  
   public void printNumSols() {
       System.out.println("There were "+numsols+" solutions.");
   }
  
}