Java 穿越迷宫时出现堆栈溢出错误
我试图递归地穿越迷宫,但我得到了一个堆栈溢出错误,我理解这个问题,但无法修复它。我是否需要创建一个单独的数组来保存数组中访问过的所有值,或者是否有其他更有效的方法使用更少的代码行 如有任何建议,将不胜感激 这是输入Java 穿越迷宫时出现堆栈溢出错误,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我试图递归地穿越迷宫,但我得到了一个堆栈溢出错误,我理解这个问题,但无法修复它。我是否需要创建一个单独的数组来保存数组中访问过的所有值,或者是否有其他更有效的方法使用更少的代码行 如有任何建议,将不胜感激 这是输入 5 6 // Row,Col 1 1 // Start Pos 3 4 // End Pos 1 1 1 1 1 1 0 0 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1 1 1 当前代码: public cl
5 6 // Row,Col
1 1 // Start Pos
3 4 // End Pos
1 1 1 1 1
1 0 0 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 1 1 1 1
当前代码:
public class Solve {
private static int [][] MazeArray;
private static int Rows;
private static int Cols;
private static Point end = new Point();
private static Point start = new Point();
public static void ReadFileMakeMaze() {
Scanner in = new Scanner(System.in);
System.out.print("Select File: "); // Choose file
String fileName = in.nextLine();
fileName = fileName.trim();
String Buffer = "";
String[] Buffer2;
String[] MazeBuffer;
int Counter = 0;
try {
// Read input file
BufferedReader ReadFileContents = new BufferedReader(new FileReader(fileName+".txt"));
Buffer = ReadFileContents.readLine();
MazeBuffer = Buffer.split(" ");
// Creating MazeArray according to rows and columns from input file.
Rows = Integer.parseInt(MazeBuffer[0]);
Cols = Integer.parseInt(MazeBuffer[1]);
MazeArray = new int[Rows][Cols];
// Retrieving start locations and adding them to an X and Y coordinate.
String[] StartPoints = ReadFileContents.readLine().split(" ");
start.x = Integer.parseInt(StartPoints[0]);
start.y = Integer.parseInt(StartPoints[1]);
// Retrieving end locations and adding them to an X and Y coordinate.
String[] EndPoints = ReadFileContents.readLine().split(" ");
end.x = Integer.parseInt(EndPoints[0]);
end.y = Integer.parseInt(EndPoints[1]);
while(ReadFileContents.ready()) {
Buffer = ReadFileContents.readLine();
Buffer2 = Buffer.split(" ");
for(int i = 0; i < Buffer2.length; i++) {
MazeArray[Counter][i] = Integer.parseInt(Buffer2[i]); // Adding file Maze to MazeArray.
}
Counter ++;
}
}
catch(Exception e){
System.out.println(e); // No files found.
}
System.out.println(SolveMaze(start.x, start.y));
}
public static boolean SolveMaze(int x,int y) {
Print(); // Printing the maze
if(ReachedEnd(x,y)) {
MazeArray[x][y] = 5; // 5 represents the end
System.out.println(Arrays.deepToString(MazeArray));
return true;
}else if(MazeArray[x][y] == 1 || MazeArray[x][y] == 8){
return false;
}else {
MazeArray[x][y] = 8; // Marking the path with 8's
start.x = x;
start.y = y;
// Checking all directions
if(MazeArray[x][y - 1] == 0 ) {
System.out.println("Left");
SolveMaze(x, y - 1);
}else if(MazeArray[x + 1][y] == 0) {
System.out.println("Down");
SolveMaze(x + 1, y);
}else if(MazeArray[x - 1][y] == 0 ) {
System.out.println("Up");
SolveMaze(x - 1, y);
}else if(MazeArray[x][y + 1] == 0 ) {
System.out.println("Right");
SolveMaze(x, y + 1);
}else {
System.out.println("Debug");
MazeArray[x][y] = 0;
start.x = x;
start.y = y;
}
}
return false;
}
public static boolean DeadEnd(int x, int y) {
return true; // Solution needed
}
public static boolean ReachedEnd(int x, int y) {
if(x == end.x && y == end.y) { // Check if the end has been reached.
return true;
}else {
return false;
}
}
public static void Print() {
System.out.println(Arrays.deepToString(MazeArray));
}
public static void main(String[] args) {
ReadFileMakeMaze();
}
}
公共类解决方案{
专用静态int[]mazarray;
私有静态int行;
私有静态int Cols;
私有静态点端点=新点();
私有静态点开始=新点();
公共静态void ReadFileMakeMaze(){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“选择文件:”;//选择文件
字符串fileName=in.nextLine();
fileName=fileName.trim();
字符串缓冲区=”;
字符串[]缓冲区2;
字符串[]MazeBuffer;
int计数器=0;
试一试{
//读取输入文件
BufferedReader ReadFileContents=新的BufferedReader(新的文件阅读器(fileName+“.txt”);
Buffer=ReadFileContents.readLine();
MazeBuffer=Buffer.split(“”);
//根据输入文件中的行和列创建MazarRay。
Rows=Integer.parseInt(MazeBuffer[0]);
Cols=Integer.parseInt(MazeBuffer[1]);
Mazarray=新整数[行][Cols];
//检索起始位置并将其添加到X和Y坐标。
String[]StartPoints=ReadFileContents.readLine().split(“”);
start.x=Integer.parseInt(StartPoints[0]);
start.y=Integer.parseInt(StartPoints[1]);
//检索端点位置并将其添加到X和Y坐标。
String[]EndPoints=ReadFileContents.readLine().split(“”);
end.x=Integer.parseInt(端点[0]);
end.y=Integer.parseInt(端点[1]);
while(ReadFileContents.ready()){
Buffer=ReadFileContents.readLine();
Buffer2=Buffer.split(“”);
for(int i=0;i
首先,正如您在问题中提到的,在SolveMaze之外创建一个静态集合,保存“已访问”节点的列表肯定会有所帮助。如果之前访问过该节点,则无需再次检查
其次,我相信上面的代码中有一些错误。该表未正确生成到MazarRay[],请在“/”下交换行和列,根据输入文件中的行和列创建MazarRay。“
代码也没有“找到”路径,数组被定义为int[]array=new int[5],但要访问它,必须使用array[0]-->array[4],当我修复迷宫时,会发生的情况是从元素1,1开始,元素1是左上角的“0”行和列。然后根据if-else语句的顺序遍历列表。访问每个节点后,将节点值设置为8。经过4次迭代后,代码将查看第二行底部的0,即您上方的[4][1]为now和8,并且由于0是您可以移动到的唯一元素,因此节点将向左、向右和向下查看并看到1,然后转向并向上查看并看到8。就这样结束了
第三,不要让检查上、下、左、右成为一组“if-else”,而只是选择“if”。这样,代码将遍历所有路径。然后,您可以保留8,它实际上不会重新运行同一节点两次,从而不需要“已访问”数组。(尽管作为一般规则,穿越时的突变状态是不可取的)
第四,我无法从上述代码中获得堆栈溢出错误,因此无法回答实际问题
另一方面,使用测试和类似于