简体   繁体   中英

Java- Maze Breadth First Search Shortest Path

I seem to be having a problem understanding how to retrive the Shortest Path that is discovered by the Breadth First Search algorithm I am implementing. Currently the Algorithm works as it can find the exit of the maze if it can reach it. In another post someone mentioned keeping tack of the parent after a node is marked as visited. I have tried a simple implementation of trying to keep track of the parent by including a parent Point inside the Point structure but when i Mark the path on the Grid it marks all the paths it has traveresed so far, I believe I may have messed up the keeping track of the parent somehow.

Any one have any clue as to where I have gone wrong. Maybe I am missing something simple? The Code is included below as a reference.

The maze is a 2D integer Grid. Assume 1 is a wall and 0 is a movable path.

The Point class contains: Point Parent int x, y; and nothing more.

public Point BFS(int x, int y){
    Queue<Point> Path = new Queue<>();

    Path.enqueue(new Point(x,y));//push current on queue

    Point Cur = Path.front();//make current point
    //test code for recursive backcall of path
        Cur.setParent(null);
    //test code for recursive backcall of path


    Point end = new Point(mWidth-2, mHeight-2);//set goal

    while((!Path.isEmpty())){
        Point old = Cur;

        Cur = Path.dequeue();
        Cur.setParent(old);

        if(Cur.compareto(end)){
            return Cur;//return Point then traverse up from here calling Cur.Parent to get path.
        }
        else if(Maze[Cur.getX()][Cur.getY()]==1){//Enque all possible paths

            Maze[Cur.getX()][Cur.getY()] = 2;//mark as visitied
            if(Cur.getX()-1>=0)
                if(Maze[Cur.getX()-1][Cur.getY()]==1)
                    Path.enqueue(new Point(Cur.getX()-1,Cur.getY()));

            if(Cur.getX()+1<mWidth)
                if(Maze[Cur.getX()+1][Cur.getY()]==1)
                    Path.enqueue(new Point(Cur.getX()+1,Cur.getY()));

            if(Cur.getY()-1>=0)
                if(Maze[Cur.getX()][Cur.getY()-1]==1)
                    Path.enqueue(new Point(Cur.getX(),Cur.getY()-1));

            if(Cur.getY()+1<mHeight)
                if(Maze[Cur.getX()][Cur.getY()+1]==1)
                    Path.enqueue(new Point(Cur.getX(),Cur.getY()+1));
        }

    }
    return null;
}

Looks like old is no longer the parent after multiple iterations. You can set the parent when adding the point to the queue instead, for example:

Point next = new Point(Cur.getX()-1,Cur.getY());
next.setParent(Cur);
Path.enqueue(next);

Then when you find the end, you should be able to get the path back to the beginning by calling getParent() until it's null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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