Java 我的2d迷宫解算器没有';我不适合做多项选择题
我想写二维阵列迷宫解算器,我明白了。但是,我的代码不适用于2d阵列贴图具有2个或更多解决方案的情况Java 我的2d迷宫解算器没有';我不适合做多项选择题,java,algorithm,maze,Java,Algorithm,Maze,我想写二维阵列迷宫解算器,我明白了。但是,我的代码不适用于2d阵列贴图具有2个或更多解决方案的情况 public class Mapsolver { private int tried = 2; private int path = 3; private int maze[][]; public Mapsolver(int maze[][], int destinationcolumn, int destinationrow, int locationcolu
public class Mapsolver {
private int tried = 2;
private int path = 3;
private int maze[][];
public Mapsolver(int maze[][], int destinationcolumn, int destinationrow, int locationcolumn, int locationrow) {
this.maze = maze;
traverse(locationrow, locationcolumn, destinationrow, destinationcolumn);
}
public boolean valid(int row, int column) {
boolean result = false;
if (row >= 0 && row < maze.length && column >= 0 && column < maze[row].length) {
if (maze[row][column] == 1) {
result = true;
}
}
return result;
}
public boolean traverse(int row, int column, int destrow, int destcolumn) {
boolean done = false;
if (valid(row, column)) {
maze[row][column] = tried;
if (row == destrow && column == destcolumn)
done = true;
else {
done = traverse(row + 1, column, destrow, destcolumn);
if (!done)
done = traverse(row, column + 1, destrow, destcolumn);
if (!done)
done = traverse(row - 1, column, destrow, destcolumn);
if (!done)
done = traverse(row, column - 1, destrow, destcolumn);
}
if (done) {
maze[row][column] = path;
}
}
return done;
}
public String toString() {
String result = "\n";
for (int row = 0; row < maze.length; row++) {
for (int column = 0; column < maze[row].length; column++)
result += maze[row][column] + "";
result += "\n";
}
return result;
}
}
公共类映射求解器{
私有int=2;
私有int路径=3;
私有int迷宫[];
公共映射解算器(int迷宫[],int目标列,int目标行,int位置列,int位置行){
这个迷宫=迷宫;
遍历(locationrow、locationcolumn、destinationrow、destinationcolumn);
}
公共布尔值有效(int行,int列){
布尔结果=假;
如果(行>=0&&row=0&&column
如果我们有一个解决方案,它是绝对正确的。但如果我们有两个或更多的解决方案,它标志着所有可能的解决方法。然而,我不想在打印时看到所有的解决方案。正确的输出将是其中一种解决方案 您用于求解迷宫的算法是a,提供的解决方案不一定是到达目的地的最短路径 递归的结束条件确保您将只收到一个解决方案。您认为的多个解决方案实际上是一个解决方案,如以下打印示例所示,基于您的代码(10*10网格,xx是墙,目的地在(6)(3),每个迷宫单元封装在“|”中,访问的单元是--”s): 另一个例子: 还有一点: 解决方案中编号的步骤表明DFS算法提供了一条通往目的地的非常长且曲折的路径
底线-您得到的解决方案比您想象的要长得多。您可以为每个访问的顶点存储指向父顶点的指针,一旦到达目标顶点,您就停止搜索并将(反向)路径打印回起始顶点。但在所谓的“完美”迷宫中,它只不过是一棵生成树,任何两个顶点之间都会有一条路径。欢迎来到StackOverflow,如果需要了解答案,请添加更多描述和代码,因为它会尽快解决其他人的问题。