2

I been working on this code for a while and can't seem to get it to work. I have started over multiple times. I am not sure if my logic is off or if I could be doing something better. Any suggestion in the right direction or new ideas to try would be helpful. I understand that a maze could be solved recursively but for this part of the assignment I need to use a stack.

I created two stacks. One for the path the other for spots I already searched. Ideally I would check to see if the searched path contains the next spot in a direction. If it does it checks another direction.

Sample 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

My algorithm seems to get stuck between 2,3 and 2,4. Never explores 1,4 or 0,4. I see it keep bouncing between 2,3 and 2,4 in an infinite loop. So it seems my searched.contains() is not functioning properly. Any suggestion to fix my searched stack? Ideally, when I run my code. I want it to check East, South, west then North has already been searched or not. If all points have been checked it will pop the last position from my path stack using current= path.pop inside the while loop and repeat.

Position is a custom class. I have considered adding a previous position variable to the constructor in the position class but seems to not to be needed if I can get my path stack to work. If I am wrong on this please let me know.

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;
}
TechWizPro
  • 33
  • 1
  • 4

2 Answers2

3

A common solution to solving a maze is BFS, which is both optimal and complete [it always find a solution if there is one, and also it finds the shortest one].

Using a stack, is actually simulating a DFS, which is not optimal.

About the specific problem, where contains() doesn't do its job, it will be hard to know what the problem is without the source for the class Position, but a possible reason is you did not override equals(), so the default equals() is still checking for identity, and not equality - which is obviously not true.

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
amit
  • 175,853
  • 27
  • 231
  • 333
  • @increment1 In this case, DFS is complete , since the number of states is finite [which is not always true] and only if you maintain `visited` set [what the OP did]. However, by doing so - you lose DFS's main advantage on BFS - memory efficiency, without gaining the advantage of optimality. Usually DFS does not run through all possible paths [there are exponential number of those!] and if you maintain a `visited` set, you miss a lot of paths, which makes the algorithm not optimal. – amit Mar 12 '12 at 21:47
3

I would guess that the problem lies, not in the code you posted, but rather in the Position class. If you have not overridden hashcode() and equals() in the Position class, the contains() comparison here will be looking for object equality.

So when you ask if searched.contains(east), having just created east as a new object, it will return false, even though east's coordinates perfectly match what is already in your search.

See this answer for more.

Community
  • 1
  • 1
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • Thank you for the answer. I checked and it was actually returning false rendering my searched stack useless. problem is solved. – TechWizPro Mar 12 '12 at 22:39