【问题标题】:java:implement 8 queen using depth first searchjava:使用深度优先搜索实现 8 Queen
【发布时间】:2014-01-31 03:03:57
【问题描述】:

我正在尝试使用深度搜索来实现 8 个皇后,它适用于空板(板上没有皇后),但如果有解决方案,我需要它在初始状态下工作,如果没有这个初始状态的解决方案它将打印没有解决方案

这是我的代码:

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;

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


}

例如对于这个初始状态,它给了我以下错误:

Exception in thread "main" java.lang.StackOverflowError

我被卡住了,我认为错误是无限调用如何解决这个问题的方法。

任何想法都会有所帮助,在此先感谢。

注意:broad是二维数组,当我输入(1)时,表示此时有女王。

注2: 我们将初始状态设置为如下所示:

       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; 

【问题讨论】:

  • 到目前为止你做了什么来分析这个问题?您是否尝试过添加打印输出来观察它正在做什么,或者在调试器中运行它,或者报告搜索深度(它不应该超过板上的单元数,对吧?),或者......
  • @keshlam 我试过了,但我想不通。

标签: java artificial-intelligence n-queens


【解决方案1】:

[编辑:添加条件输出提示。]

添加到@StephenC 的答案:

这是一段复杂的代码,尤其是如果您没有 Java 编程经验。

我执行了你的代码,它一遍又一遍地输出(一遍又一遍)

back at (0,0)
 01234567
0
1 Q
2  Q
3   Q
4    Q
5     Q
6      Q
7       Q
back at (0,0)

然后崩溃了

Exception in thread "main" java.lang.StackOverflowError
    at java.nio.Buffer.<init>(Unknown Source)
    ...
    at java.io.PrintStream.print(Unknown Source)
    at java.io.PrintStream.println(Unknown Source)
    at Depth.eightQueen(Depth.java:56)
    at Depth.eightQueen(Depth.java:60)
    at Depth.eightQueen(Depth.java:60)
    at Depth.eightQueen(Depth.java:60)
    at Depth.eightQueen(Depth.java:60)
    ...

我的第一直觉总是添加一些 System.out.println(...)s 以​​找出哪里出了问题,但这在这里行不通。

我看到的唯一两个选项是

  • 熟悉调试器,并使用它来逐步分析和分析为什么它永远不会停止循环
  • 打破它,伙计!你怎么能希望处理这样一个巨大的问题而不把它分解成可消化的块???

更不用说8-queens这个概念一开始就很复杂。


进一步思考:

System.out.println()s 在当前实现时没有用,因为输出是无限的。调试器是这里更好的解决方案,但另一种选择是以某种方式限制您的输出。例如,在顶部创建一个计数器

private static final int iITERATIONS = 0;

而不是

System.out.println("[ANUMBERFORTRACING]: ... USEFUL INFORMATION ...")

使用

conditionalSDO((iITERATIONS < 5), "[ANUMBERFORTRACING]: ... USEFUL INFORMATION");

函数如下:

private static final void conditionalSDO(boolean b_condition, String s_message)  {
   if(b_condition)  {
       System.out.println(s_message);
   }
}

另一种选择是不限制输出,而是限制write it to a file

希望这些信息对您有所帮助。

(注意:我将 OP 的代码编辑为可编译。)

【讨论】:

  • 奇怪的板位来自main我想。 (我无法理解为什么 OP 将板初始化为明显不正确的解决方案......)。 FWIW,应该可以用大约 50 行 Java 编写解决方案;例如请参阅 Wikipedia 示例解决方案。
  • @alliteralmind 我使用 System.out.println(...)s 来找出哪里出了问题,但我可以知道哪里出了问题?
  • @aliteralmind 感谢您的回复,但您能帮帮我吗。
  • 刚刚添加了上面的答案。再看看。
  • @aliteralmind 我知道错误在哪里,但我不知道如何解决它,请帮忙。
【解决方案2】:

您询问了关于如何解决它的想法(与解决方案不同!)所以,这里有一些提示:

提示 #1:

如果您在递归程序中得到StackOverflowError,它可能意味着以下两种情况之一:

  • 你的问题太“深”了,或者
  • 您的代码中有一个错误导致它无限地递归

在这种情况下,问题的深度很小(8),所以这一定是一个递归错误。

提示 #2:

如果您检查堆栈跟踪,您将看到堆栈中每个调用的方法名称和行号。这...以及一些想法...应该可以帮助您找出代码中的递归模式(已实现!)。

提示 #3:

使用调试器 Luke ...

提示 #4:

如果您希望其他人阅读您的代码,请更加注意样式。您的缩进在最重要的方法中搞砸了,并且您犯了(IMO)不可原谅的罪过,即忽略了标识符的 Java 样式规则。方法名必须以小写字母开头,类名必须以大写字母开头。

(原则上我很快就停止阅读你的代码了。)

【讨论】:

  • 我花了很多时间试图解决它,但我被卡住了,请帮忙。
【解决方案3】:

尝试在for (x = 0; x &lt; board.length - 1; x++) 所在的行中更改您的方法IsValid

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 2022-06-13
    相关资源
    最近更新 更多