使用堆栈解决迷宫

时间:2012-03-12 21:28:58

标签: java algorithm maze

我一直在研究这段代码,似乎无法让它发挥作用。我已经多次开始了。我不确定我的逻辑是否关闭或者我是否可以做得更好。任何正确方向的建议或尝试的新想法都会有所帮助。我知道迷宫可以递归地解决,但是对于这部分的分配,我需要使用堆栈。

我创建了两个堆栈。一个用于路径,另一个用于我已经搜索过的斑点。理想情况下,我会检查搜索到的路径是否包含方向上的下一个点。如果确实如此,它会检查另一个方向。

样本迷宫

0 1 0 1 0
0 0 0 1 0
0 1 0 0 0
0 1 0 1 1
0 1 0 0 0

我的算法似乎陷入2,3和2,4之间。永远不要探索1,4或0.4。我看到它在无限循环中在2,3和2,4之间反弹。所以看来我的searching.contains()运行不正常。有什么建议来修复我的搜索堆栈?理想情况下,当我运行我的代码。我希望它检查东,南,西,然后北方已经被搜查过。如果已经检查了所有点,它将使用while循环中的current = path.pop从我的路径堆栈中弹出最后一个位置并重复。

位置是一个自定义类。我已经考虑过将一个前一个位置变量添加到位置类的构造函数中,但是如果我可以让我的路径堆栈工作,似乎根本不需要它。如果我错了,请告诉我。

public static Position [] stackSearch(char [] [] maze){
    //todo: your path finding algorithm here using the stack to manage search list
    //your algorithm should modify maze to mark positions on the path, and also
    //return array of Position objects coressponding to path, or null if no path found
     ArrayDeque <Position> path = new ArrayDeque<Position>();
     ArrayDeque <Position> searched = new ArrayDeque<Position>();


        //creates position object
        Position start = new Position(0,0,'0');
        Position current;
        Position north,south, east, west;

        int i = 0; int j = 0;
        //push (0,0) onto stack
        path.push(start);
        searched.push(start);
        while(!path.isEmpty()){
            current=path.pop();
            i=current.i;
            j=current.j;
            if(i==maze.length-1 && j==maze.length-1 &&  maze[i][j]=='0'){
                 Position[] trail= new Position [path.size()];
                while(!path.isEmpty()){
                    for(int k=0; k<path.size();k++){
                    trail[k]=path.pop();
                }
                    return trail;
            }
        }
            System.out.println(i +"," +j);

            //check east.
            east= new Position(i,j+1,'0');
            south= new Position(i+1,j,'0');
            west= new Position(i,j-1,'0');
            north= new Position(i-1,j,'0');
            if (j+1 >= 0 && j+1 < maze.length  && maze[i][j+1] == '0' && searched.contains(east)==false)
            {


                    searched.push(east);
                    path.push(current);
                    path.push(east);




            }
            //check south, add its position to the list.

            else if (i+1 >= 0 && i+1 < maze.length &&  maze[i+1][j] == '0' && searched.contains(south)==false)
            {


                    searched.push(south);
                    path.push(current);
                    path.push(south);


            }
          //check west.

             else if (j-1 >= 0 && j-1 < maze.length  && maze[i][j-1] == '0' && searched.contains(west)==false)
            {


                    searched.push(west);


                    path.push(current);
                    path.push(west);

            }              
            //check north

             else if (i-1 >= 0 && i-1 < maze.length &&  maze[i-1][j] == '0' && searched.contains(north)==false)
            {


                    searched.push(north);
                    path.push(current);
                    path.push(north);


            }

       }
    return null;
}

2 个答案:

答案 0 :(得分:3)

解决迷宫的常见解决方案是 BFS optimalcomplete [如果有的话,总会找到解决方案,并且它也找到最短的一个]。

使用堆栈实际上是模拟DFS,这不是最佳的。

关于特定问题contains()无法正常工作,如果没有类Position的来源,很难知道问题是什么,但是可能的原因是你没有覆盖equals() ,所以默认的equals()仍在检查身份,而不是相等 - 这显然不是真的。

答案 1 :(得分:3)

我猜测问题不在于您发布的代码,而在于Position类。如果您未在Position类中重写hashcode()equals(),则此处的contains()比较将寻找对象相等。

因此,当您询问searched.contains(east)是否刚创建east作为新对象时,即使east的坐标与搜索中已有的坐标完全匹配,也会返回false

有关详情,请参阅this answer

相关问题