【问题标题】:java sudoku backtracking recursivejava数独回溯递归
【发布时间】:2013-11-13 02:42:49
【问题描述】:

嘿,我编写了这个程序来解决数独问题,但它只适用于数独矩阵的几个单元格,而其他单元格返回 0 。你能理解这有什么问题吗? 我是java编码新手,不能写一个简单的程序真的很痛苦。

public class sudoku {

static int sud[][] = new int[9][9];

public static void main(String args[]) {

    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            sud[i][j] = 0;
        }
    }
    solve(sud);
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            System.out.print(sud[i][j]);
        }
        System.out.print("\n");
    }
}

public static boolean solve(int[][] sud) {
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            if (sud[i][j] != 0) {
                continue;
            }
            for (int x = 1; x < 10; x++) {
                if (!used(i, j, x)) {
                    sud[i][j] = x;
                    if (solve(sud)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
    return true;

}

public static boolean isinrow(int i, int j, int x) {
    for (int t = 0; t < 9; t++) {
        if (sud[i][t] == x) {
            return true;
        }
    }
    return false;
}

public static boolean isincol(int i, int j, int x) {
    for (int t = 0; t < 9; t++) {
        if (sud[t][j] == x) {
            return true;
        }
    }
    return false;

}

public static boolean isinsq(int sr, int sc, int x) {
    for (sr = 0; sr < 3; sr++) {
        for (sc = 0; sc < 3; sc++) {
            if (sud[sr][sc] == x) {
                return true;
            }
        }
    }
    return false;
}

static boolean used(int i, int j, int x) {
    if (!isinrow(i, j, x)) {
        if (!isincol(i, j, x)) {
            if (!isinsq(i - (i % 3), j - (j % 3), x)) {
                return false;
            }
        }
    }
    return true;
}

}

【问题讨论】:

  • 这是输出:123456789 456789000 000000000 000000000 000000000 000000000 000000000 000000000 000000000
  • 评论是你的朋友。立即学习这一课!

标签: java recursion sudoku backtracking


【解决方案1】:

您的问题在于您正在执行的此功能

 public static boolean isinsq(int sr, int sc, int x) {
    for ( sr = 0; sr < 3; sr++) {
         //  ^ you are reseting the value of sr that you pass in
          //   effectivel making it so that you always check the first square
        for (  sc = 0; sc < 3; sc++) {
               // ^ same here    
            if (sud[sr][sc] == x) {
                return true;
            }
        }
    }
    return false;
}

这会将 sr 重置为 0,因此您只检查了左上角而不是您传入的坐标。您应该执行以下操作:

public static boolean isinsq(int xcorner, int ycorner, int x) {
    for (int sr = xcorner; sr < 3; sr++) {
           //^ here w create a new variable with the starting value that you passed in
        for ( int sc = ycorner; sc < 3; sc++) {
                   //^ here w create a new variable with the starting value that you passed in
            if (sud[sr][sc] == x) {
                return true;
            }
        }
    }
    return false;
}

顺便说一句,您在求解中的 Double 嵌套 for 循环是不必要的,并且会增加大量开销。与其寻找下一个开放点,为什么不假设它们都是开放的并检查它们是否不是这样,您的递归将为您处理迭代。考虑这样的事情(它也更简单......至少对我来说)

bool solve(int[][] sud, int x, int y)
{
//here need to make sure that when x>9 we set x to 0 and increment y
//also if x == 9 and y == 9 need to take care of end condition

if(sud[x][y]==0) //not a pre given value so we are ok to change it
{
    for(int i =1; i<10; ++i)
    {
        sud[x][y] = i;
        if(validBoard(sud)) //no point in recursing further if the current board isnt valid
        {
            if(solve(sud, x+1,y))
            {
                return true;
            }
        }
    }
}
else
{
    return solve(x+1,y);
}
return false;
}

我留下了一些需要填补的地方(让我为你做这件事有什么乐趣:)。正如其他人所建议的那样,您应该使用更有意义的变量名。

【讨论】:

  • 因为 if (!isinsq(i - (i % 3), j - (j % 3), x)) 会检查所有的方块,我按照你说的做了,但我的输出是一样的:(
  • 哦,我明白了!现在它的输出更好:))
【解决方案2】:

在我看来isInSq 总是会检查左上角的方块,因为当你进入循环时你会清除你的参数。您应该从sr 转到sr + 2,sc 也是如此。我认为这不是唯一的问题,但它看起来是一个不错的起点。

作为另一个提示,我会修复您的变量名称。你也许能够理解它们,但它会让其他人更难阅读。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多