我正在尝试递归地实现骑士之旅。下面是我的代码。为简单起见,我选择了 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(深度优先搜索)相同,因为它们基本上具有相同的想法?
先感谢您。