1

UPDATED CODE: My last question is how do I get it so that my program prints all 92 solutions of an 8x8 board, without any Queens attacking each other? So far, my code only prints 1 solution. For example, when I change it to a 4x4 board, I only have 1 solution, when there should be 2 I believe.

public class Queen{

   public static void display(int[] board){
      int n = board.length;
      for(int column = 0; column < n; column++){
         for(int row = 0; row < n; row++){
            if(board[column] == row)
               System.out.print('Q');
       else 
         System.out.print('x');



         }
         System.out.print('\n');
      }
   }

    public static int[] solveQueens(int n){
        int board[] = new int[n];
        placeQueen(board,0);
        return board;
    }
    private static boolean placeQueen(int[] board, int column){
        int n = board.length;
        if (n == column){
            return true;
        }else{
            for (int row = 0; row < n; row++){
                int i; //remove
                for (i = 0; i < column; i++){  //for (int i)
                    if (board[i] == row || i - column == board[i] - row || column - i == board[i] - row){
                        break;
                    }
                }
                if (i == column){ 
                    board[column] = row;
                    if (placeQueen(board, column + 1))
                        return true;
                }
            }
        }
        return false;
    }
    public static void main(String args[]){
        int finished[] = solveQueens(8);
        display(finished);
        }
    }

Now my programs returns:

Qxxxxxxx
xxxxQxxx
xxxxxxxQ
xxxxxQxx
xxQxxxxx
xxxxxxQx
xQxxxxxx
xxxQxxxx

OLD CODE: I need to use recursive backtracking to solve the 8-queens problem. The n-queens problem is a puzzle that requires placing n chess queens on an n × n board so that none of the queens attack each other.

I need to Write a public solveQueens(int n) method to solve the problem for an nxn board

I also need to write a private recursive placeQueen(board, column) method to attempt to place a queen in the specified column.

This is my code so far:

public class Queen{

    public static int[] solveQueens(int n){
      int board[] = new int[n];
       int finished[] = placeQueen(board,0);
       return finished;

    }
    private static int[] placeQueen(int[] board, int column){
       int n = board.length;
       int row = column;
       if (n == column){
          return board;
       }else{ 
          for (row = n; row < n; row++){
            board[column] = row;
             if (board[n] == 0 && board[n-1] == 0 && board[n+1] == 0){
               board[n] = 1;
                placeQueen(board, column+1);
             }
          }
         for (row = n; row < n; row++){
             board[row] = column; 
            if (board[n] == 0 && board[n-1] == 0 && board[n+1] == 0){
                board[n] = 1;
                placeQueen(board, column+1);
             }
          }
       }
       return board;
    }
    public static void main(String args[]){
       int finished[] = solveQueens(8);
       for (int item: finished){
          System.out.print(item);
       }
    }
}

Whenever I run my program, all it returns is

----jGRASP exec: java Queen
00000000

Is there any explanation on how to setup my 8x8 board, and how to place queens so they don't attack each other?

coder
  • 60
  • 2
  • 9
  • You have two for loops like `for (row = n; row < n; row++){`.These will not run since the condition is false from the outset. Did you mean `row = 0`? – Ole V.V. Dec 08 '16 at 05:28
  • If you don’t know how to use your debugger (1) learn, you will need (2) until you learn, stick in `System.out.println()` statements to track the excution of your code and inform you of the value of some of the variables. – Ole V.V. Dec 08 '16 at 05:29
  • there's lots more than just one solution board for this puzzle. – Will Ness Dec 08 '16 at 09:14
  • and it's best to have comments in your code so it is understandable right away. – Will Ness Dec 08 '16 at 10:38
  • @WillNess I have updated code and my problem is that I want to see all 92 solutions for an 8x8 board, but my code only prints 1 solution. – coder Dec 08 '16 at 22:31
  • instead of returning it, print it, and continue the loop. Or, instead of returning, append the solution to the list of solutions; then when the loops are finished, print each solution in this list. – Will Ness Dec 08 '16 at 23:36
  • so System.out.print(board) instead of return(board) ? – coder Dec 08 '16 at 23:42
  • Possible duplicate of [Puzzled about backtracking in Eight Queen](http://stackoverflow.com/questions/17926069/puzzled-about-backtracking-in-eight-queen) – coder Dec 09 '16 at 02:05

1 Answers1

1

I made some changes in solveQueens and placeQueen:

public class Queen{

    public static int[] solveQueens(int n){
        int board[] = new int[n];
        placeQueen(board,0);  //it fills the board
        return board;
    }
    private static boolean placeQueen(int[] board, int column){
        int n = board.length;
        int row;
        if (n == column){
            return true;
        }else{
            for (row = 0; row < n; row++){
                int c;
                for (c=0; c<column; c++){
                    if (board[c]==row || c-column==board[c]-row || column-c==board[c]-row){
                        //check if it is safe position (we know 2 queens couldn't place in a same column or row or diameter
                        break;
                    }
                }
                if (c==column){   //if previous loop didn't break...=> it is safe position
                    board[column]=row;
                    if (placeQueen(board, column+1))   //if it is possible to fill the rest of board //else: test the next row
                        return true;
                }
            }
        }
        return false;
    }
    public static void main(String args[]){
        int finished[] = solveQueens(8);
        for (int item: finished){
            System.out.print(item);
        }
    }
}
Zahra Rhmn
  • 26
  • 3
  • Would you know how to print out the entire 8x8 board? Such as x x x x x x x x x x with 8 rows as well? – coder Dec 08 '16 at 19:29
  • You can change main function and add a display function: – Zahra Rhmn Dec 08 '16 at 19:58
  • public static void display(int[] board) { int n = board.length; for(int c=0; c – Zahra Rhmn Dec 08 '16 at 19:58
  • Thanks man! Really appreciate it! – coder Dec 08 '16 at 21:53
  • I do have one more question though. Lets say that I try to solve for an 8x8 board. This program should provide me with roughly 92 solutions, I believe. However, this code only provides me with one solution. Is there somewhere in my code where I need to display the board, instead of returning it? – coder Dec 08 '16 at 22:00
  • 1
    yes! in placeQueen function, in `if (n == column){ return true; }` you can call display method, instead of return true, and return false! so it shows you all of the answers... – Zahra Rhmn Dec 09 '16 at 16:00