Java: обобщенная 8 королева для работы в любом начальном состоянии с использованием поиска в глубину

Я пытаюсь решить проблему с 8 ферзями для любого начального состояния (поместить 8 ферзей в любое место на доске), но это дает мне исключение stackoverflow.

вот мой код:

import java.io.IOException;

public class depth {
public static void main(String[] args) {

   //we create a board
   int[][] board = new int[8][8];
   board [0][0]=1;
   board [1][1]=1;
   board [2][2]=1;
   board [3][3]=1;
   board [4][4]=1;
   board [5][5]=1;
   board [6][6]=1;
   board [7][7]=1;

 IOException e = null;
   eightQueen(8, board, 0, 0, false);
   System.out.println("the solution as pair");
   for(int i=0;i<board.length;i++){
       for(int j=0;j<board.length;j++)
           if(board[i][j]!=0)

           System.out.println("  ("+i+" ,"+j +")");

   System.out.println("the number of node stored in memory "+count1);

   }

   e.printStackTrace();

   }

   public static int count1=0;
   public static void eightQueen(int N, int[][] board, int i, int j, boolean found) {

   long startTime = System.nanoTime();//time start

   if (!found) {
    if (IsValid(board, i, j)) {//check if the position is valid
        board[i][j] = 1;

        System.out.println("[Queen added at (" + i + "," + j + ")");
        count1++;
        PrintBoard(board);

        if (i == N - 1) {//check if its the last queen
            found = true;
            PrintBoard(board);
            double endTime = System.nanoTime();//end the method time

            double duration = (endTime - startTime)*Math.pow(10.0, -9.0);
            System.out.print("total Time"+"= "+duration+"\n");
        }
        //call the next step
        eightQueen(N, board, i + 1, 0, found);
    } else {

        //if the position is not valid & if reach the last row we backtracking
        while (j >= N - 1) {
            int[] a = Backmethod(board, i, j);
            i = a[0];
            j = a[1];

            System.out.println("back at (" + i + "," + j + ")");
            PrintBoard(board);
        }
        //we do the next call
        eightQueen(N, board, i, j + 1, false);
    }
    }

   }

  public static int[] Backmethod(int[][] board, int i, int j) {
  int[] a = new int[2];
  for (int x = i; x >= 0; x--) {
    for (int y = j; y >= 0; y--) {
        //search for the last queen
        if (board[x][y] != 0) {
            //deletes the last queen and returns the position
            board[x][y] = 0;
            a[0] = x;
            a[1] = y;
            return a;
        }
    }
  }
  return a;
}

public static boolean IsValid(int[][] board, int i, int j) {

int x;
//check the queens in column
for (x = 0; x < board.length; x++) {
    if (board[i][x] != 0) {
        return false;
    }
}
//check the queens in row
for (x = 0; x < board.length; x++) {
    if (board[x][j] != 0) {
        return false;
    }
}
//check the queens in the diagonals
if (!SafeDiag(board, i, j)) {
    return false;
}
return true;
}

public static boolean SafeDiag(int[][] board, int i, int j) {

int xx = i;
int yy = j;
while (yy >= 0 && xx >= 0 && xx < board.length && yy < board.length) {
    if (board[xx][yy] != 0) {
        return false;
    }
    yy++;
    xx++;
}
xx = i;
yy = j;
while (yy >= 0 && xx >= 0 && xx < board.length && yy < board.length) {
    if (board[xx][yy] != 0) {
        return false;
    }
    yy--;
    xx--;
}
xx = i;
yy = j;
while (yy >= 0 && xx >= 0 && xx < board.length && yy < board.length) {
    if (board[xx][yy] != 0) {
        return false;
    }
    yy--;
    xx++;
}
xx = i;
yy = j;
while (yy >= 0 && xx >= 0 && xx < board.length && yy < board.length) {
    if (board[xx][yy] != 0) {
        return false;
    }
    yy++;
    xx--;
   }
   return true;
 }
 public static void PrintBoard(int[][] board) {
System.out.print(" ");
for (int j = 0; j < board.length; j++) {
    System.out.print(j);
}
System.out.print("\n");
for (int i = 0; i < board.length; i++) {
    System.out.print(i);
    for (int j = 0; j < board.length; j++) {
        if (board[i][j] == 0) {
            System.out.print(" ");
        } else {
            System.out.print("Q");
        }
    }
    System.out.print("\n");
   }
  }

  }

и вывод:

at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at depth.eightQueen(depth.java:68)

странно, что это работает для следующего начального состояния:

   board [0][0]=1;
   board [1][1]=1;
   board [2][2]=1;
   board [3][3]=1;
   board [4][4]=1;
   board [5][5]=1;
   board [6][6]=1;
   board [7][1]=1;

person john    schedule 31.01.2014    source источник
comment
какое исключение?   -  person DaveH    schedule 31.01.2014
comment
половина трассировки стека отсутствует/   -  person njzk2    schedule 31.01.2014
comment
также, в какой строке происходит сбой?   -  person njzk2    schedule 31.01.2014
comment
for any initial state что ты имеешь в виду?   -  person njzk2    schedule 31.01.2014
comment
@ njzk2 я отредактировал выше в своих вопросах.   -  person john    schedule 31.01.2014
comment
@njzk2 сбой в строке 60, stackoverflow   -  person john    schedule 31.01.2014
comment
судя по внешнему виду вашего алгоритма, он скорее предназначен для ввода пустой доски, поскольку он сразу же пытается разместить ферзей, не ища ни одного из присутствующих.   -  person njzk2    schedule 31.01.2014
comment
@njzk2 как это исправить?   -  person john    schedule 31.01.2014