Siren88 0 Newbie Poster

Hi,
My assignment is to write static methods to give the following output:

- Lists of all the sequences, all the perms, all the sets, and all the multisets with n=5 and k=3.
-The N Queens Problem considers how many ways n queens can be placed on an n by n checkerboard such that no two queens attack each other. Think about how to represent one placement of n queens, then write methods to find and count up all the solutions for any n. How many solutions are there when n=4? n=8? n=10?
Hint: Use Perm to represent candidate solutions as permutations.

I wrote my program for n=8 and the solution came out to be 5242. I looked up the real solution online and it says for the 8-queens problem, it has 92 solutions. I don't know what I did wrong. Any help will be appreciated. Thank you!

/* Assignment 4: Write code to find all solutions to 
 * the n-queens problem.  
 *
 * As written below, n is passed 
 * as an argument on the command-line. eg:
 *
 *    java NQueens 8
 *
 * should print the number of solutions to the 8-queens 
 * problem.  You're welcome to hard-code values of n 
 * instead if that's easier for you.
 *
 * Feel free to add additional methods as you see fit.
 */

public class NQueens {
 
 

    public static void main(String args[]) {
     int n = 8;
     if (args.length>=1)
      n = Integer.parseInt(args[0]);
        int numSolutions = 0;

        int perms[] = new int[n];
        for (int i =0; i<n; i++){
         perms[i] = i+1;
        }
        
       
        do{
         //NQueens.printArray(perms);
         if (NQueens.isSolution(perms))
          numSolutions++;
         perms = NQueens.iterate(perms);
        }while(!NQueens.isLast(perms));

        System.out.println("The " + n + "-queens problem has "
                           + numSolutions + " solutions.");
    }
    
 private static boolean isLast(int[] values) {
  boolean isLast= true;
  for (int i=0; i<values.length; i++){
   if (values[i] != values.length-i)
    isLast = false;
  }
  return isLast;
 }
 
 private static void printArray(int[] values){
  System.out.print("Array value: ");
  for (int i=0; i<values.length; i++){
   System.out.print(values[i] + " ");
  }
  System.out.println();
 }
 
 private static boolean isSolution(int[] values){
  boolean isSolution = true;
  for (int i= 0; i<values.length-1; i++){
   if ((values[i]+1) == (values[i+1]) || (values[i]-1) == (values[i+1]))
    isSolution = false;
  }
  return isSolution;
 }
 
 private static int[] iterate(int[] values){
  int l = -1;
  int j = -1;
  int k = values.length;
     for (int i=k-1; i>=1;i--){
      if (values[i-1] < values[i]){
       j = i-1;
       break;
      }
     }
     if(j!=-1){
      for (int i=k-1; i>=j;i--){
       if (values[j] < values[i]){
        l=i;
        break;
       }
      }
      
      int tempVal = values[j];
         values[j] = values[l];
         values[l] = tempVal;
         //System.out.println("k="+k+" j+1=" + (j+1));
         int tempArray[] = new int[k-(j+1)];
         //System.out.println("length" + tempArray.length);
         for(int i=0; i<tempArray.length; i++){
          tempArray[i] = values[(j+1)+i];
         }
         //NQueens.printArray(tempArray);
         for(int i=j+1; i<k; i++){
          values[i] = tempArray[(tempArray.length-1)-(i-(j+1))];
         }
         return values;
     }
     else
      return values;
 }
}