java:使用深度优先搜索的8个皇后

java:使用深度优先搜索的8个皇后,java,n-queens,Java,N Queens,我实现了对8 queen的深度优先搜索,它对空板工作良好,但我需要修改它以接受任何初始状态。我修改了它,但它给出了一个错误。我不知道怎么纠正这个 这是我的密码: public class depth { public static int count1=0; public static void eightQueen(int N, int[][] board, int i, int j, boolean found) { long startTime = Sys

我实现了对8 queen的深度优先搜索,它对空板工作良好,但我需要修改它以接受任何初始状态。我修改了它,但它给出了一个错误。我不知道怎么纠正这个

这是我的密码:

public class depth {
    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");
        }
    }

    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;


        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);    
    }
}

它不适用于任何初始状态。我不知道问题在哪里,如果找不到任何解决方案,我需要它打印“无解决方案”。

方法
eightQueen
无限期地调用自身。

此代码导致死锁,同一函数不断被递归调用。 方法:eightQueen检查位置(i,j)是否有效。在您的例子中,i和j的第一个值分别为(0,0)

它增加j直到达到7,然后返回第一个皇后位置(0,0)。else循环从不增加i,并且所有可用位置都不安全,因此它最终会进入无限循环


您需要重新查看代码。

没有人会坐在这里为您浏览所有代码。这就是为什么上帝创造了调试器。如果堆栈溢出,则递归方法没有退出条件,它将永远调用自己。我使用调试器我知道错误,但我不知道如何向在关闭投票队列中遇到此问题的人更正此注释:即使这是最先发布的,我投票关闭这一个,因为第二个有一个可接受的答案。感谢回复,如何更正此问题。我不知道如何更正此问题,您可以帮助我吗?您的代码要求至少有一个可用位置。在您的测试场景中,所有8个初始位置都需要更改,因为第一女王没有任何位置。当前的代码将适用于所有条件,如{1,1,1,1,1,1,1,0}您可以帮助我编辑代码以打印没有解决方案如果它没有任何放置皇后的位置,谢谢。
 Exception in thread "main" java.lang.StackOverflowError