简体   繁体   English

找到所有路径(和最短路径)的迷宫算法

[英]maze algorithm to finding all path (and shortest path)

Hi everyone in the this code i want to find all path to exit the maze in some case for one exit home you have more than one path i want to select shortest path how should i add this future to below code, the below code is my algorithm to find path,but it is not find all path 大家好,在此代码中,我想找到所有退出迷宫的路径,在某种情况下,一个退出家门,你有多个路径,我想选择最短路径,我该如何将这个未来添加到下面的代码中,下面的代码是我的找到路径的算法,但不是找到所有路径

public void searchPath(char[][] BW, int i , int j ){   // megdar avalie i = SX-1 ,megdar avalie j= SY-1

        if(i<0 || i>(N-1)) return;     // check out of bound
        if(j<0 || j>(N-1)) return;
        if(BW[i][j] == 'b') return;     // if cell is black return
        if(visit[i][j]== true) return;

        visit[i][j] = true;

        path.add(i+","+j);

        if( !(i==SX-1 && j==SY-1 )){
            if(i==0 || i==N-1 || j==0 || j==N-1) {

                    if(first){
                        //first = false;
                        drawWay(path);
                    }                       
            }
        } 
        searchPath(BW , i , j+1);          // right

        searchPath(BW , i-1 , j);          // search the up side of cell    

        searchPath(BW , i , j-1);          // left
        searchPath(BW , i+1 , j);           // search the down          

        path.remove(path.size()-1);        // remove the not necceccery path


    }

I am not sure if I got this right - first you said that you want to find all possible paths to exit the maze, but then you mentioned finding the shortest one. 我不确定我是否正确-首先,您说您想找到所有可能的路径来退出迷宫,但是随后您提到了找到最短的路径。

Actually, both of your problems can be solved with the same approach. 实际上,两个问题都可以用相同的方法解决。 Your are currently using depth first search in your algorithm ( http://en.wikipedia.org/wiki/Depth-first_search ). 您当前正在算法中使用深度优先搜索( http://en.wikipedia.org/wiki/Depth-first_search )。 It goes as deep as possible and when there's no other way to go, it returns back - hence the name. 它越深越好,并且当没有其他方法可走时,它会返回-故名。

What you should use instead is breadth first search ( http://en.wikipedia.org/wiki/Breadth-first_search ). 相反,您应该使用广度优先搜索( http://en.wikipedia.org/wiki/Breadth-first_search )。 This way, you'll traverse the maze in layers - in ith "round", you'll visit all positions that are i squares away from home square. 这样,您将在迷宫中逐层遍历-在ith “回合”中,您将访问i距离家园正方形的所有位置。 So when you now get to the border square, it has to be the shortest possible path. 因此,当您现在到达边界广场时,它必须是最短的路径。 BFS can be implemented using queue. BFS可以使用队列来实现。

To find all possible solutions, you'll have to use extra space in order to remember from which position did you get to the current one. 要找到所有可能的解决方案,您必须使用额外的空间,以便记住从哪个位置到达当前位置。 Then, when you'll want to find the way back home from position i, j, you'll write current position to output, then look into the array of previous positions for i, j. 然后,当您要查找从位置i,j返回的原路时,您需要将当前位置写入输出,然后查看i,j先前位置的数组。 Now you have predecessors of i, j - k, l and do the same with them - output k, l and look into the array for predecessors of k, l. 现在,您具有i,j-k,l的前任者,并对其进行相同的操作-输出k,l并查看数组中k,l的前任者。 Repeat this until you "get back home". 重复此过程,直到您“回到家”。

I would post you a bit of a code, but I don't know Java so I don't want to confuse you. 我会向您发布一些代码,但是我不懂Java,所以我不想让您感到困惑。 ;) ;)

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

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