【问题标题】:Knight's tour recursion doesn't find a solution骑士的巡回递归没有找到解决方案
【发布时间】:2022-01-03 00:36:15
【问题描述】:

之前有人问过骑士之旅,但我仍然遇到问题。我正在尝试递归访问棋盘的所有单元格,但访问次数不能超过 52 次。之后,它回溯并且访问的单元格的数量倒计时。这是我的代码:

public class Ch7E22_3 {
    public static int[][] chess;
    public static int[][] adjacent;

    public static void main(String[] args) {
        chess = new int[8][8];
        adjacent = new int[][] { { 2, 1 }, { 2, -1 }, { -2, 1 }, { -2, -1 }, { 1, 2 }, { -1, 2 }, { 1, -2 },
                { -1, -2 } };
        initializeChess();
        move(1, 0, 0);
    }

    private static void move(int cnt, int row, int col) {
        chess[row][col] = cnt;
        if (cnt == (8 * 8)) {
            System.out.println("You moved around all cells: " + cnt);
        } else {
            for (int i = 0; i < 8; i++) {
                if (((row + adjacent[i][0] >= 0) && (row + adjacent[i][0]) < 8)
                        && ((col + adjacent[i][1] >= 0) && (col + adjacent[i][1] < 8))) {
                    if (chess[row + adjacent[i][0]][col + adjacent[i][1]] == 0) {
                        row = row + adjacent[i][0];
                        col = col + adjacent[i][1];
                        cnt++;
                        System.out.println(row + "  " + col + "  cnt = " + cnt);
                        move(cnt, row, col);
                    }
                }
            }
        }
        chess[row][col] = 0;
    }

    private static void initializeChess() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                chess[i][j] = 0;
            }
        }
    }
}

【问题讨论】:

  • 不确定,但初始化的 int 数组不是已经用零填充了吗?所以 main 方法中的 initializeChess() 是不必要的。
  • @FatTony 没错;只需要清除一个已经使用过的数组并重新开始,这个程序不会这样做。

标签: java recursion graph-algorithm backtracking knights-tour


【解决方案1】:

第一个问题在这里:

for (int i = 0; i < 8; i++) {

所以,你有那个 for 循环。假设您在 51 处输入 cnt。

现在你有

if ( ... some overly complicated condition ... ) {
  ... cnt++
  move(cnt,

现在发生的情况是:首先您进行递归调用,并且可能每次都正确地执行您的第一次如果命中。因此,您增加 cnt 并再次递归。因此,您的打印输出显示 cnt 是如何不断增加的。

但在某些时候,递归结束 - if 条件不再成立。所以递归调用的方法...结束。但请记住:您调用了该方法......从另一个调用 move。而且那个可能仍在循环中。更重要的是,that move() 有其自己的 版本的cnt。

要明白我的意思:将 cnt 从 方法本地 变量更改为您的类的字段,类似于您的板!当你这样做时,你会发现你的代码实际上是打印出来的:

...
3  7  cnt = 52
2  4  cnt = 53
...
2  3  cnt = 64
You moved around all cells: 64
0  4  cnt = 65
...

稍后用

停止
4  0  cnt = 126

意思:打印cnt只是你的算法不能正常工作的副作用!

最后:我建议将您的 if 条件分解为一组小的辅助方法,例如

private static isXyz(...)

并在 if 语句中调用这些方法 - 这将极大地提高可读性!

【讨论】:

  • 看来cnt 只是对当前访问过的方格(单元格)的计数,而不是曾经访问过的所有单元格,因此它不应超过 64。
  • 如果我将cnt 视为一个类字段,那么我的类中有两个计数器。其中一个是类字段cnt,另一个是移动方法参数private static void move(int counter, int row, int col) { chess[row][col] = cnt;
  • 所以我想你的意思是我应该在每次递归调用时计算“计数器”。
  • 我的意思是:你应该确定你打算做什么,然后你去实现它。它是您的代码,它是您的计数器,因此您应该知道该计数器为何存在以及它的用途。
  • @GhostCat 好的!感谢您的帮助。
【解决方案2】:

您的回溯不正确。到最后一行执行时,将 chess[row][col] 设置回零,row 和 col 几乎肯定发生了变化。

最好不要在您的方法中更改row、col 和cnt,而只将新值传递给递归调用。像这样的东西,虽然我还没有检查你的其他逻辑:

    for (int i = 0; i < 8; i++) {
        int nextrow = row + adjacent[i][0];
        int nextcol = col + adjacent[i][1];
        if (nextrow >= 0 && nextrow < 8 &&
                nextcol >= 0 && nextcol < 8) {
            if (chess[nextrow][nextcol] == 0) {
                System.out.println(nextrow + "  " + nextcol + "  cnt = " + (cnt+1));
                move(cnt+1, nextrow, nextcol);
            }
        }
    }

为确保您在仅修改传递给后续递归调用的值时保持一致,并且从不在方法中修改它们,您可以将参数设为 final:

private static void move(final int cnt, final int row, final int col) {
    ...
}

【讨论】:

  • 如果我把cnt作为最终变量,我以后就数不过来了。这将是一个常数。
  • @Behnaz 我可能误解了您如何使用cnt;我以为你想在移动时增加它,但在回溯时返回它的先前值?
  • 是的。对不起,如果我让你误解了。
  • 我试过你的建议,我放cnt+1时遇到无限循环
  • 如果我把cnt作为最终变量,我以后就数不过来了。这将是一个常数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 2020-10-28
  • 1970-01-01
相关资源
最近更新 更多