简体   繁体   English

递归迷宫求解器方法问题

[英]Recursive Maze Solver method Issue

Given a 2 dimensional char array filled with 0's and 1 where 0 represents a wall and 1 represents a valid path, I have developed a recursive method called findPath(int r, int c) to find the exit in the maze marked with an 'x'. 给定一个用0和1填充的二维char数组,其中0表示墙,1表示有效路径,我开发了一种名为findPath(int r,int c)的递归方法,以在迷宫中找到标记为'x的出口”。 The method takes in the current row and column of the maze and goes through N,E,S,W directions until it finds a valid path and marks that valid path with a '+'. 该方法将进入迷宫的当前行和列,并经过N,E,S,W方向,直到找到有效路径并将该有效路径标记为“ +”。 Given an instance where all directions are found to be blocked by a wall, the method then is suppose to backtrack until this is not the case anymore, and then marking that path traveled with an 'F' to symbolize the bad path. 给定一个实例,发现所有方向都被墙堵住了,那么该方法应该回溯直到不再如此,然后用“ F”标记该路径,以表示该路径不正确。

Right now I can't figure out why the findPath method doesn't seem to transverse through all the directions as my display method just shows the program starting from the coordinates I pass in and not moving anywhere from there, why could this be? 现在,我无法弄清为什么findPath方法似乎并不能横向遍历所有方向,因为我的显示方法只是显示了从我传入的坐标开始而不是从那里移动的程序,为什么会这样呢?

Here is my Driver class 这是我的司机课

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);


    }
}

And here is my maze solver class with the findPath method 这是我的带有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();
    }
}

Your algorithm has several flow in itself, which I don't feel right to point out. 您的算法本身具有多个流程,我不认为这是正确的。 You can search for maze traverse problems, and get many good tutorials. 您可以搜索迷宫遍历问题,并获得许多不错的教程。

However, give attention to the method calls. 但是,请注意方法调用。 Notice that if findPath(int r, int c) get called with findPath(5, 5) then a call to findPath(r, c++) passes the values findPath(5, 5) again, not with findPath(5, 6) . 请注意,如果使用findPath(5, 5) findPath(int r, int c)调用findPath(int r, int c) findPath(5, 5)则对findPath(r, c++)的调用将findPath(r, c++)传递值findPath(5, 5) ,而不是findPath(5, 6)

Because in that case findPath(r, c++) get called with current value of c and after that c++ gets executed. 因为在那种情况下, findPath(r, c++)会以c当前值调用,然后执行c++

Same goes for findPath(r, c--) findPath(r++ , c) etc, etc. findPath(r, c--) findPath(r++ , c)等也是如此。

A good idea to understand the fact is to print the values int r, int c at the starting of method findPath() . 了解事实的一个好主意是在方法findPath()的开始处打印值int r, int c Also play a little with post increments/decrements(x++/--x) and pre increments/decrements(++x/--x). 也可以使用后期增量/减量(x ++ /-x)和前置增量/减量(++ x /-x)玩一点。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM