【问题标题】:Recursive Maze Solver method Issue递归迷宫求解方法问题
【发布时间】:2017-09-12 01:38:57
【问题描述】:

给定一个用 0 和 1 填充的二维 char 数组,其中 0 代表一堵墙,1 代表一条有效路径,我开发了一种称为 findPath(int r, int c) 的递归方法来找到标有迷宫的出口一个'x'。该方法获取迷宫的当前行和列,并通过 N、E、S、W 方向,直到找到有效路径并用“+”标记该有效路径。给定一个所有方向都被墙挡住的实例,然后假设该方法回溯,直到不再是这种情况,然后用“F”标记经过的路径以表示坏路径。

现在我无法弄清楚为什么 findPath 方法似乎没有横穿所有方向,因为我的显示方法只是显示程序从我传入的坐标开始并且没有从那里移动到任何地方,为什么会这样是吗?

这是我的驱动程序类

public class MazeMain2
{   
    public static void main(String[]args)
    {

    char[][] mazeArr = {{'0','0','0','1','0','0','0','0','0','0','0','0','0','0','0'},
                {'0','0','0','1','0','0','0','0','1','0','0','0','0','1','0'},
                {'0','0','0','1','1','1','1','1','1','1','1','1','0','0','0'},
                {'0','0','0','1','0','0','0','0','0','0','0','1','0','0','0'},
                {'0','0','0','1','1','1','1','1','0','0','0','1','0','0','0'},
                {'0','0','0','0','0','0','0','1','0','0','0','1','0','0','0'},
                {'0','0','0','0','1','1','1','1','0','0','0','1','0','0','0'},
                {'0','0','0','0','1','0','0','1','0','0','0','1','0','1','0'},
                {'0','0','0','0','1','0','0','1','0','0','0','0','0','0','0'},
                {'0','0','0','0','1','0','0','0','0','0','0','0','0','0','0'},
                {'0','0','0','0','1','1','1','1','1','1','1','0','0','0','0'},
                {'0','0','0','0','0','0','0','0','0','0','1','0','0','0','0'},
                {'0','0','0','0','0','0','0','0','0','0','1','0','0','0','0'},
                {'0','0','0','0','0','1','0','0','0','0','1','1','1','1','0'},
                {'0','0','0','0','0','0','0','0','0','0','1','0','0','0','0'}};

    MazeSolver2 mazeS = new MazeSolver2(mazeArr);

    mazeS.markEntry();
    mazeS.markExit();

    mazeS.solve(0, mazeS.start);


    }
}

这是我使用 findPath 方法的迷宫求解器类

public class MazeSolver2
{
    int start;
    int exit;

    char[][] maze;

public MazeSolver2(char[][] currentMaze)
{
    maze = currentMaze;
}

//Finds where the first 1 is in the top row of the 
//maze (entrance)
public void markEntry()
{
    for(int x = 0; x < maze.length; x++)
    {
        if(maze[0][x] == '1')
        {
            maze[0][x] = 'E';
            start = x;
        }
    }
}

//Finds where the last 1 is in the bottom row of the 
//maze (exit)
public void markExit()
{
    for(int x = 0; x < maze.length; x++)
    {
        if(maze[maze.length - 1][x] == '1')
        {
            maze[maze.length - 1][x] = 'x';
            exit = x;
        }
    }
}

public void solve(int x, int y)
{
    if(findPath(x, y))
    {
        System.out.println(maze[x][y]);
    }
    else
        System.out.println("No solution");

}

public boolean findPath(int r, int c)
{   
    displayMaze(maze);

    //Found the exit
    if(maze[r][c] == 'x')
    {
        return true;
    }


    if(maze[r][c] == '0' || maze[r][c] == '+' || maze[r][c] == 'F')
    {
        return false;
    }

    maze[r][c] = '+';

    //If row is currently at zero then don't check north
    //direction because it will be outside of the maze
    if(r <= 0)
    {
        if(findPath(r, c++))
        {
            return true;
        }


        if(findPath(r++, c))
        {
            return true;
        }

        if(findPath(r, c--))
        {
            return true;
        }

    }

    else
    {
        //check N, E, S, W directions
        if(findPath(r--, c) || findPath(r, c++) ||
            findPath(r++, c) || findPath(r, c--))
        {
            return true;
        }
    }

    //Marking the bad path
    maze[r][c] = 'F';

    return false;

}

//Displays maze
public void displayMaze(char[][] maze)
{

        for(int row = 0; row < maze.length; row++)
        {
            for(int col = 0; col < maze.length; col++)
            {
                if(col == 14)
                {
                    System.out.print(maze[row][col]);
                    System.out.println();
                }
                else
                {
                    System.out.print(maze[row][col]);
                }
            }
        }   

    System.out.println();
    }
}

【问题讨论】:

  • 您是post increment operator 的受害者。始终在新行上递增。
  • 仅供参考 - 如果您的入口点位于 (0, 0),您的代码将引发异常。查看if(findPath(r, c--))

标签: java arrays recursion


【解决方案1】:

您的算法本身有几个流程,我觉得指出这些流程是不对的。可以搜索迷宫穿越问题,得到很多不错的教程。

但是,请注意方法调用。请注意,如果findPath(int r, int c) 被findPath(5, 5) 调用,那么对findPath(r, c++) 的调用将再次传递值findPath(5, 5),而不是findPath(5, 6)。

因为在这种情况下,findPath(r, c++) 被调用,当前值为 c,然后c++ 被执行。

findPath(r, c--) findPath(r++ , c) 等也是如此。

了解事实的一个好主意是在方法findPath() 的开头打印值int r, int c。 还可以使用后增量/减量(x++/--x)和前增量/减量(++x/--x)。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    相关资源
    最近更新 更多