0

我正在尝试递归地实现骑士之旅。下面是我的代码。为简单起见,我选择了 5x5 板。我的目标是打印出正确的骑士位置,并可能在打印语句的同时显示回溯。

class Knight
{
public boolean[][] board = new boolean[5][5];

public final int SQUARES = 5*5-1;

public Knight()
{

    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            board[i][j] = false;

}

public boolean tour(int x, int y, int visitedX, int visitedY,int n)// x,y coords, just visited x,y and counter n
{

    if(x>=5||y>=5||x<0||y<0){ //out of bounds
        System.out.println("Visited "+x+", "+y+" Out of bounds");
        System.out.println("Back to "+visitedX+", "+visitedY);
        return false;
    }
    else{
        if(board[x][y] == true)
        {return false;}
        if(board[x][y]!=true){
            boolean result = false;
            board[x][y]=true;
            System.out.println("Visited "+x+", "+y);
            result = tour(x+2,y+1,x,y,n+1); 
            result = tour(x+2,y-1,x,y,n+1); 
            result = tour(x+1,y-2,x,y,n+1); 
            result = tour(x-1,y-2,x,y,n+1); 
            result = tour(x+1,y+2,x,y,n+1); 
            result = tour(x-1,y+2,x,y,n+1);
            result = tour(x-2,y-1,x,y,n+1); 
            result = tour(x-2,y+1,x,y,n+1); 

        }
    }
    return false;
}
}
public class KnightTApp {

public static void main(String[] args) {
    Knight k = new Knight();
    k.tour(0,0,0,0,0);

}

}

打印输出的一部分

Visited 4, 0
Visited 6, 1 Out of bounds
Back to 4, 0
Visited 6, -1 Out of bounds
Back to 4, 0
Visited 5, -2 Out of bounds
Back to 4, 0
Visited 3, -2 Out of bounds
Back to 4, 0
Visited 5, 2 Out of bounds
Back to 4, 0
Visited 2, -1 Out of bounds
Back to 4, 0
Visited 2, 0

我的问题是在这个过程的中间,我在索引 4,0 处遇到了一个死胡同(不是所有的方块都被覆盖,但没有我可以做的合法动作)。而且由于我没有包含任何回溯行。它只是跳转到索引 2,0,这甚至不是合法的骑士移动。

我的问题是如何用 if 语句表达死胡同?我想我应该将递归设置为某种布尔值并从那里开始,但我不知道该怎么做。

并且使用递归是否与使用 DFS(深度优先搜索)相同,因为它们基本上具有相同的想法?

先感谢您。

4

2 回答 2

3

您需要弄清楚您是在尝试识别所有可能的旅行,还是只是为了找到一个有效的旅行。

要获得完整的回溯,您需要在游览结束时返回呼叫者时再次将棋盘字段标记为空闲。

其次,您需要检查每个递归调用的结果tour()。如果为真,你找到了一个有效的星座,你也可以返回真。否则尝试下一个可能的动作。如果没有移动,则返回 false。

最后你错过了一个适当的终止条件:“什么是成功?” 在您的情况下,如果您有5x5成功的移动,则所有字段都已访问,您可能只是返回true表示成功的信号。

您可能还想跟踪移动的顺序,以便有机会输出成功的游览。

上面的目标是确定一个成功的旅行。
通过跟踪移动序列,您可以跟踪所有成功的游览,而不是仅在最深的递归检查已知游览时返回 true,如果是新游览则返回 true。

作为参考,您的代码的修改版本添加了“查找任何”部分:

class Knight {
    public final static int SIZE = 5;

    public boolean[][] board = new boolean[SIZE][SIZE];
    public String[] moves = new String[SIZE * SIZE];

    public Knight() {

        for (int i = 0; i < 5; i++ ) {
            for (int j = 0; j < 5; j++ ) {
                board[i][j] = false;
            }
        }

    }

    public boolean tour(final int x, final int y, final int n)
    {
        if (n >= SIZE * SIZE) {
            return true;
        } else if (x >= 5 || y >= 5 || x < 0 || y < 0) { // out of bounds
            return false;
        } else if (board[x][y]) {
            return false;
        } else {
            // System.out.println("Checking " + n + ": " + x + "," + y);
            board[x][y] = true;
            moves[n] = x + "-" + y;
            if (tour(x + 2, y + 1, n + 1)) {
                return true;
            }
            if (tour(x + 2, y - 1, n + 1)) {
                return true;
            }
            if (tour(x + 1, y - 2, n + 1)) {
                return true;
            }
            if (tour(x - 1, y - 2, n + 1)) {
                return true;
            }
            if (tour(x + 1, y + 2, n + 1)) {
                return true;
            }
            if (tour(x - 1, y + 2, n + 1)) {
                return true;
            }
            if (tour(x - 2, y - 1, n + 1)) {
                return true;
            }
            if (tour(x - 2, y + 1, n + 1)) {
                return true;
            }
            board[x][y] = false;
            moves[n] = null;
            return false;
        }
    }

    public static void main(final String[] args) {
        final Knight k = new Knight();

        for (int i = 0; i < SIZE; i++ ) {
            System.out.println("Starting at " + i + " 0");

            if (k.tour(i, 0, 0)) {
                k.printTour(true);
                break;
            }
            k.printTour(true);
        }
    }

    /**
     * @param result
     */
    private void printTour(final boolean result) {
        System.out.println("Found tour: " + result);
        int i = 0;
        if (result) {
            for (final String m : moves) {
                System.out.println("M-" + i + ": " + moves[i]);
                i++ ;
            }
        }
    }
}
于 2016-03-23T20:23:17.070 回答
0

您可能希望使用 Stack 数据结构,并在每次移动时评估所有可能的有效移动,并将每个移动推入堆栈。当您进行有效移动时,将其从堆栈中弹出。

当您的评估方法返回 0 个可能的移动时,就会出现死胡同。

于 2016-03-23T20:21:45.807 回答