java - 迷宫寻路,二维数组

标签 java arrays maze

我正在尝试使用堆栈来穿过迷宫。

在我担心堆栈之前,我会尝试让它首先进入死胡同。

但是,当我运行代码时,我得到 java.lang.ArrayIndexOutofBoundsException: -1 。这是没有意义的,因为我不断更新行和列。

这是我所坚持的部分代码:

    Maze myMaze = new Maze (rows, columns);
    MazeDisplay myDisplay = new MazeDisplay (myMaze);

    myMaze.buildMaze(10);
    myMaze.setSolveAnimationDelay(75);

    boolean [][] mazeArray = new boolean [rows][columns];

    for (int row=0; row<mazeArray.length; row++)
        for (int col=0; col<mazeArray[row].length; col++)
            mazeArray[row][col] = false;
            mazeArray [myMaze.getCurrentRow()][myMaze.getCurrentCol()] = true;


    for (int i = 0; i < 9999999 ; i++) //Temporary  for now 
    {
        int arrayRows = myMaze.getCurrentRow();
        int arrayCols = myMaze.getCurrentCol();

        if (myMaze.isOpen(Maze.Direction.RIGHT) && mazeArray [arrayRows][1 + arrayCols] == false)
        {
            myMaze.move(Maze.Direction.RIGHT);
            //mazeStack.push(Maze.Direction.RIGHT);
            mazeArray[arrayRows][arrayCols] = true;
        }
        else if (myMaze.isOpen(Maze.Direction.UP) && mazeArray [1 + arrayRows][arrayCols] == false)
        {
            myMaze.move(Maze.Direction.UP);
            //mazeStack.push(Maze.Direction.UP);
            mazeArray[arrayRows][arrayCols] = true;
        }
        else if (myMaze.isOpen(Maze.Direction.LEFT) && mazeArray [arrayRows][arrayCols - 1] == false) // <---Getting an error here.
        {
            myMaze.move(Maze.Direction.LEFT);
            //mazeStack.push(Maze.Direction.LEFT);
            mazeArray[arrayRows][arrayCols] = true;
        }
        else if (myMaze.isOpen(Maze.Direction.DOWN) && mazeArray [arrayRows - 1][arrayCols] == false) // <---Getting an error here.
        {
            myMaze.move(Maze.Direction.DOWN);
            //mazeStack.push(Maze.Direction.DOWN);
            mazeArray[arrayRows][arrayCols] = true;
        }

有办法解决这个问题吗?我想要的是访问 ArrayList 当前位置下方或左侧的位置。

以下是 Maze 类的一些方法:

//-------- isOpen - returns true if there is no wall in the direction that is passed in
public boolean isOpen(Direction direction)
{
    boolean result = false;
    if (direction == Direction.UP && mazeArray[currentArrayRow-1][currentArrayCol]==0)
        result = true;
    else if (direction == Direction.DOWN && mazeArray[currentArrayRow+1][currentArrayCol]==0)
        result = true;
    else if (direction == Direction.LEFT && mazeArray[currentArrayRow][currentArrayCol-1]==0)
        result = true;
    else if (direction == Direction.RIGHT && mazeArray[currentArrayRow][currentArrayCol+1]==0)
        result = true;

    return result;
}

//-------- getCurrentRow - returns the current (real) row
public int getCurrentRow()
{
    return currentArrayRow/2;
}

//-------- getCurrentCol - returns the current (real) col
public int getCurrentCol()
{
    return currentArrayCol/2;
}

// -------- move - receives a Direction and moves there if OK.  Calls the other
//                 arrayMove to do this
public boolean move(Direction direction)
{
    boolean success = false;

    if (direction == Direction.UP)
        success = arrayMove(currentArrayRow-2, currentArrayCol);
    else if (direction == Direction.DOWN)
        success = arrayMove(currentArrayRow+2, currentArrayCol);
    else if (direction == Direction.LEFT)
        success = arrayMove(currentArrayRow, currentArrayCol-2);
    else if (direction == Direction.RIGHT)
        success = arrayMove(currentArrayRow, currentArrayCol+2);

    return success;
}

//This is Maze's enumerated data type: moves can be UP, DOWN, LEFT, RIGHT
public enum Direction
{
    UP, DOWN, LEFT, RIGHT
}

//-------- getMazeArray - returns the mazeArray
public int[][] getMazeArray()
{
    return mazeArray;
}

最佳答案

在检查左侧单元格之前,您应该检查您是否不在左侧边缘。否则 mazeArray [arrayRows][arrayCols - 1] 将抛出异常,因为 arrayCols - 1 = -1。

如果我正确理解你的代码,那么你的算法并不完美。它陷入了死胡同。我认为shortest path算法是最容易实现的一种。

关于java - 迷宫寻路,二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36290200/

相关文章:

arrays - 如何检查变量是否在 PowerShell 中的数组中?

javascript - 仅打印单击的单选按钮值(React)

javascript - 为什么 Angular 会将索引 0 数组插入子数组而不是索引 1?

java - 如何找到所有可用的迷宫路径?

java - Xpath - 问题,使用 Firebug 检测元素

java - java中使用迭代器?

Java 列表<对象> 大小

java - 暂停和恢复正在 hibernate 的 Java 线程

java - 更改二维迷宫求解器以与一维迷宫一起使用

algorithm - 在邻接表中表示墙