在java中使用堆栈解决迷宫

在java中使用堆栈解决迷宫,java,stack,backtracking,maze,Java,Stack,Backtracking,Maze,我知道这是一个常见的话题,但其他帖子都不能帮助我完成我的程序 在这个迷宫中,我们必须读取一个文件,第一行有我们数组的宽度和高度,第二行有起始点E的坐标,第三行是MazeRunner数组的起始点。我必须遵循所有0,直到找到出口 以下是一个例子: 8 7 0 3 1 1 1 1 1 E 1 1 0 0 0 1 0 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 0 1 0 1 0 1 1 1 1 在下面的代码中

我知道这是一个常见的话题,但其他帖子都不能帮助我完成我的程序

在这个迷宫中,我们必须读取一个文件,第一行有我们数组的宽度和高度,第二行有起始点
E
的坐标,第三行是
MazeRunner
数组的起始点。我必须遵循所有
0
,直到找到出口

以下是一个例子:

8 7
0 3
1 1 1 1 1 E 1
1 0 0 0 1 0 1
1 0 1 0 0 0 1
1 1 1 0 1 1 1
1 0 0 0 0 0 1
1 0 1 1 1 0 1
1 1 0 0 0 0 1
0 1 0 1 1 1 1
在下面的代码中,readFile方法工作正常。我在想如何利用堆栈(在另一个文件中定义)时遇到了一个问题。solveMaze方法开始时很好,但当它到达一个点时,在当前点附近有2个或更多的零,它只是沿着它找到的第一个方向,从而导致一个无限循环。显然,我找不到回溯的方法

Coords类也在另一个文件中定义,它只创建一个点

import java.io.*;

public class Maze{

private static boolean foundExit = false; /* Decides if the maze has a possible exit or not. */
private static int row, col;
private static Stack<Coords> stack = new Stack<Coords>();
private static char[][] MazeRunner;
private static int width, height;
private static int rowCoord,colCoord;

public static void readFile(){
    File file = null;
    BufferedReader reader = null;
    int counter=0;

    try {
        file = new File("C:/Users/user01/workspace/p1/src/thiseas.txt");
    } catch (NullPointerException e) {
        System.err.println("File not found.");
    }

    try {
        reader = new BufferedReader(new FileReader(file));
    } catch (FileNotFoundException e) {
        System.err.println("Error opening file!");
    }
    try{
        String line=reader.readLine();
        String[] split=line.split(" ");
        height = Integer.parseInt(split[0]);
        width = Integer.parseInt(split[1]);
        counter++;
        line=reader.readLine();
        split=line.split(" ");
        rowCoord=Integer.parseInt(split[0]);
        colCoord=Integer.parseInt(split[1]);
        counter++;
        MazeRunner=new char[height][width];
        char[] ch;
        while((line=reader.readLine())!=null){
            ch = line.toCharArray();
            for(int i = 0;i < 2*width-1;i+=2){
                MazeRunner[counter-2][i/2] = ch[i];
            }
            counter++;
        }

    }catch (IOException e) {
        System.out.println("Error reading line " + counter + ".");
    }

    solveMaze(rowCoord, colCoord);
}

private static void solveMaze(int a, int b) {
    row = a;
    col = b;
    Coords coor=new Coords(row,col); //coordinations
    stack.push(coor);

    int j=0; //counts how many '0' are near our pointer

    while (foundExit == false) {

        if (col > 0 && MazeRunner[row][col-1] == '0') { //goes left
            if(col-1 == 0) {
                foundExit = true;
            }else {
                coor.setCol(col-1);
                j++;
                stack.push(hold);
            }

        }
        if (col < width && MazeRunner[row][col+1] == '0') { //goes right
            if(col+1 == width) {
                foundExit = true;
            }else if(j==0){
                coor.setCol(col+1);
                j++;
                stack.push(hold);
            }
        }
        if (row > 0 && MazeRunner[row-1][col] == '0') { //goes up
            if(row-1 == 0) {
                foundExit = true;
            }else {
                coor.setRow(row-1);
                j++;
                stack.push(hold);
            }

        }
        if(row<height && MazeRunner[row+1][col] == '0'){ //goes down
            if(row+1 == height) {
                foundExit = true;
            }else{
                coor.setRow(row+1);
                j++;
                stack.push(hold);
            }

        }
        row = coor.getRow(); 
        col = coor.getCol();
        //row and col will get wrong values because hold changes
        coor= stack.peek();
    }
    System.out.println("The maze has been solved!. ");
    System.out.println("Exit coordinations: " + coor.toString());
}

public static void main(String[] args) {
    readFile(); 
}

}
import java.io.*;
公共类迷宫{
private static boolean foundExit=false;/*决定迷宫是否有可能的出口*/
私有静态整数行,列;
私有静态堆栈=新堆栈();
私有静态字符[][]MazeRunner;
私有静态int宽度、高度;
私有静态int rowCoord,colCoord;
公共静态void readFile(){
File=null;
BufferedReader reader=null;
int计数器=0;
试一试{
file=新文件(“C:/Users/user01/workspace/p1/src/thiseas.txt”);
}捕获(NullPointerException e){
System.err.println(“未找到文件”);
}
试一试{
reader=newbufferedreader(newfilereader(file));
}catch(filenotfounde异常){
System.err.println(“打开文件时出错!”);
}
试一试{
字符串行=reader.readLine();
String[]split=line.split(“”);
高度=整数.parseInt(拆分[0]);
宽度=整数.parseInt(拆分[1]);
计数器++;
line=reader.readLine();
拆分=行。拆分(“”);
rowCoord=Integer.parseInt(拆分[0]);
colCoord=Integer.parseInt(split[1]);
计数器++;
MazeRunner=新字符[高度][宽度];
char[]ch;
而((line=reader.readLine())!=null){
ch=line.toCharArray();
对于(int i=0;i<2*width-1;i+=2){
MazeRunner[counter-2][i/2]=ch[i];
}
计数器++;
}
}捕获(IOE异常){
System.out.println(“错误读取行”+计数器+”);
}
solveMaze(rowCoord,colCoord);
}
专用静态无效数据(int a、int b){
行=a;
col=b;
Coords coor=新坐标(行,列);//坐标
堆栈推送(coor);
int j=0;//计算指针附近有多少个“0”
while(foundExit==false){
如果(col>0&&MazeRunner[row][col-1]=='0'){//向左移动
if(col-1==0){
foundExit=true;
}否则{
coor.setCol(col-1);
j++;
堆叠。推动(保持);
}
}
如果(列0&&MazeRunner[row-1][col]='0'){//上升
如果(第1行==0){
foundExit=true;
}否则{
第二排(第1排);
j++;
堆叠。推动(保持);
}
}

if(row)if(row)if(row)if(row)if(row)if(row)if(row)if)if(row)if(row)if)if(row)if)if(row)if)if(row)if)if)if(row)if。不需要堆栈/递归。虽然效率不高,但编写代码很容易。只需一段时间循环和几个if/elsestatements@Araymer7好吧,我不明白这是怎么回事。这只会找到一个解决方案,如果所有的“0”从起点离开,否则它将返回错误的协调。您只需使sur在你的左边总是有一堵墙,如果前面有墙,你就向左拐,然后再直走。如果那里有墙,就向左拐,再直走,等等……你会沿着一些广场往回走,但最终你会找到一条出路。只需跟踪你“面对”的方向还有一个布尔条件,关于左边的墙是否存在(当它们弹出时总是左转)如果你想要一个超级简单和天真的方法,只要始终沿着左边的墙从起点一直走到一个边界(这意味着你找到了出口)。不需要堆栈/递归。虽然效率不高,但编写代码很容易。只需一段时间循环和几个if/elsestatements@Araymer7好吧,我不明白这是怎么回事。这只会找到一个解决方案,如果所有的“0”从起点离开,否则它将返回错误的协调。您只需使sur在你的左边总是有一堵墙,如果前面有墙,你就向左拐,然后再直走。如果那里有墙,就向左拐,再直走,等等……你会沿着一些广场往回走,但最终你会找到一条出路。只需跟踪你“面对”的方向以及关于左侧的墙是否存在的布尔条件(当它们弹出时总是左转)