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

标签 java n-queens

我对 8 个皇后实现了深度优先搜索,它对于空棋盘工作得很好,但我需要修改它以接受任何初始状态。我修改了它,但它给出了一个错误。我不知道如何纠正这个问题。

这是我的代码:

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

这是错误:

 Exception in thread "main" java.lang.StackOverflowError

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

最佳答案

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

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

您需要重新访问代码。

关于java:8 个皇后使用深度优先搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460806/

相关文章:

.net - "Put N Queens",是否可以在 N = 20 的情况下在可接受的时间内运行?

algorithm - 如何使用回溯法求解 M<N 时的 M 个皇后

java - 如何用多个分隔符分隔字符串?

java - WebView issue new update android 操作系统

java - N * N 皇后算法获取坐标

java - N-Queens 皇后是安全的无限循环

python - Python 中 n 皇后难题的 2 种解决方案之间的差异

java - 如何在 javadoc 中编写 */

java - 这个类是不可变的吗?

java - 我如何检查 ImageView 是否为空