java:implement 8 queen using depth first search

后端 未结 3 1828
你的背包
你的背包 2020-12-12 06:09

i am try to implement 8 queen using depth search for any initial state it work fine for empty board(no queen on the board) ,but i need it to work for initial state if there

相关标签:
3条回答
  • 2020-12-12 06:44

    Try to alter your method IsValid in the lines where for (x = 0; x < board.length - 1; x++).

    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;
    }
    
    0 讨论(0)
  • 2020-12-12 07:03

    You asked for ideas on how to solve it (as distinct from solutions!) so, here's a couple of hints:

    Hint #1:

    If you get a StackOverflowError in a recursive program it can mean one of two things:

    • your problem is too "deep", OR
    • you've got a bug in your code that is causing it to recurse infinitely.

    In this case, the depth of the problem is small (8), so this must be a recursion bug.

    Hint #2:

    If you examine the stack trace, you will see the method names and line numbers for each of the calls in the stack. This ... and some thought ... should help you figure out the pattern of recursion in your code (as implemented!).

    Hint #3:

    Use a debugger Luke ...

    Hint #4:

    If you want other people to read your code, pay more attention to style. Your indentation is messed up in the most important method, and you have committed the (IMO) unforgivable sin of ignoring the Java style rules for identifiers. A method name MUST start with a lowercase letter, and a class name MUST start with an uppercase letter.

    (I stopped reading your code very quickly ... on principle.)

    0 讨论(0)
  • 2020-12-12 07:04

    [EDIT: Added conditional output tip.]

    To add to @StephenC's answer:

    This is a heck of a complicated piece of code, especially if you're not experienced in programming Java.

    I executed your code, and it outputs this over and over and over and over (and over)

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

    And then crashes with this

    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)
        ...
    

    My first instinct is always to add some System.out.println(...)s to figure out where stuff is going wrong, but that won't work here.

    The only two options I see are to

    • Get familiar with a debugger and use it to step through and analyze why it's never stopping the loop
    • Break it down man! How can you hope to deal with a massive problem like this without breaking it into digestible chunks???

    Not to mention that the concept of 8-queens is complicated to begin with.


    One further thought:

    System.out.println()s are not useful as currently implemented, because there's infinite output. A debugger is the better solution here, but another option is to somehow limit your output. For example, create a counter at the top

    private static final int iITERATIONS = 0;
    

    and instead of

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

    use

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

    Here is the function:

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

    Another alternative is to not limit the output, but to write it to a file.

    I hope this information helps you.

    (Note: I edited the OP's code to be compilable.)

    0 讨论(0)
提交回复
热议问题