簡體   English   中英

遞歸迷宮求解器方法問題

[英]Recursive Maze Solver method Issue

給定一個用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();
    }
}

您的算法本身具有多個流程,我不認為這是正確的。 您可以搜索迷宮遍歷問題,並獲得許多不錯的教程。

但是,請注意方法調用。 請注意,如果使用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)

因為在那種情況下, findPath(r, c++)會以c當前值調用,然后執行c++

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

了解事實的一個好主意是在方法findPath()的開始處打印值int r, int c 也可以使用后期增量/減量(x ++ /-x)和前置增量/減量(++ x /-x)玩一點。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM