我想找到一个迷宫的路径,下面是代码,它假设进入recursiveSolve循环,但它保持退出,第二后如果条件我在做什么错在这里可以有人帮助我请? 即时设置Washere和correctpath数组默认为false。迷宫算法路径查找器
recursiveSolve(0, 0);
public static int[,] maze = {{0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 0},
{0, 1, 1, 0, 0, 1},
{0, 1, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 1}};
public static Boolean recursiveSolve(int row, int col) {
Boolean[,] wasHere = new Boolean[6, 6];
Boolean[,] correctPath = new Boolean[6, 6]; // The solution to the maze
if (maze[row, col] == 1 || wasHere[row, col]) {
return false;
}
else if (row == 0 || row == 6 - 1 || col == 0 || col ==6 - 1) {
correctPath[row, col] = true;
return true;
}
else {
wasHere[row, col] = true;
if (recursiveSolve(row - 1, col) || recursiveSolve(row + 1, col) ||
recursiveSolve(row, col - 1) ||
recursiveSolve(row, col +1)) {
correctPath[row, col] = true;
return true; // successfully escaped; this square is on path
}
else {
return false;
}
}
}
您是否尝试过调试?你会看到你的第二个条件是真的,导致函数退出。 – 2014-10-08 12:43:01
注意你的else if中的条件:else if(row == 0 || row == 6 - 1 || col == 0 || col == 6 - 1)',因为你用' recursiveSolve(0,0)''row == 0'和'col == 0'条件都是真的,你退出函数。你是不是有意从迷宫的边缘开始? – 2014-10-08 12:52:00
@MattBurland我刚刚通过这可能是出发点,什么是起点的参数? – Milas 2014-10-08 12:59:54