# Maze Solver with Stacks

• October 3rd, 2012, 06:07 PM
kovynjd2010
Maze Solver with Stacks
I need to create a maze solver using a stack (not recursively). The maze always starts at [0][0] and ends at [mazeSize-1][mazeSize-1]. 0=open path, 1=wall.
How can I make my program walk the maze and push the path to a stack (the col & row # of the maze array) and pop the stack when it hits the wall and try a different direction?

Here's what I have (it's totally wrong):
Code :

```public class DoMaze { private int mSize, x, y; int[][] maze; LinkedStack<String> stack;   public DoMaze() { x = 0; y = 0; stack = new LinkedStack<String>(); }   public void setMaze(int[][] m, int size) { maze = m; mSize = size;   walk(); }   public void walk() { stack.push(""+x+","+y);   while (!isDone()) { if (stack.isEmpty()) break;   moveRt(); stack.push(""+x+","+y); if (isBlocked()) stack.pop();   moveDwn(); stack.push(""+x+","+y); if (isBlocked()) stack.pop();   moveLt(); stack.push(""+x+","+y); if (isBlocked()) stack.pop();   moveUp(); stack.push(""+x+","+y); if (isBlocked()) stack.pop();   }   if (stack.isEmpty()) { System.out.println("No solutions found!"); } else { while (!stack.isEmpty()) System.out.println(stack.pop()); } }   public boolean isDone() { if (x == mSize-1 && y == mSize-1) return true;   return false; }     public boolean isBlocked() { return (maze[x][y] == 1); }   public void moveDwn() { if (y+1 < mSize) y=y+1; }   public void moveUp() { if (y-1 > -1) y=y-1; }   public void moveLt() { if (x-1 > -1) x=x-1; }   public void moveRt() { if (x+1 < mSize) x=x+1; }   }```
• October 3rd, 2012, 06:18 PM
jps
Re: Maze Solver with Stacks
Quote:

(it's totally wrong)
What makes you say it's totally wrong? What does it do or not do?

Your question kinda says, "give me the code that makes my program do what I want it to".
Try to describe what the program does correctly,
what it does incorrectly,
where in the code (marked with comments in the code) the program seems to show undesired operation,
any opinion you have as to what you think is wrong,
and finally describe the correct behavior you are trying to get in the end.

Breaking the question down into something like this can even lead to discovering the problem and solution on your own. But even if it does not, it does assist others in figuring out the trouble (usually much faster) and offering advice.
• October 3rd, 2012, 06:32 PM
kovynjd2010
Re: Maze Solver with Stacks
When I run it, my pc starts to freeze up like it's in an infinite loop so I have no clue what it does correctly. I don't know how to make it walk the maze while pushing and popping the stack.