So I am trying to create a maze solver program that would solve a maze of X's and O's. What I would like to do is create a class of Points, so that I can create a 2-Dimensional array of Points which would allow printing to an output page as well as implementing the stack to be relatively simple.
The simplest algorithm of the general idea I'd like to implement in the actual program itself I believe should be:
1) Move forward
2) Are you at a wall?
2a) If yes, turn left
3) Are you at the finish?
3a) If no, go to 1
3b) If yes, solved
But I'm having trouble coming up with a more in-depth algorithm, as well as getting my Points class situated. I know for Points I should have set X coordinate, and set Y coordinate as well as getters for the two as well. Do you think I need more methods than those two? Like, should I create a method that is passes an x coord, and y coord as parameters so I can just push those together as one, instead of setting x and y individually?
This is what a sample maze would look like, where you start in the bottom right and try to traverse to the top left, with X's as walls, and O's as open spaces in the maze:
O O O O O X O
X X O X O O X
O X O O X X X
X X X O O X O
X X X X O O X
O O O O O O O
X X O X X X O
Are you sure your algorithm would solve any maze? I think it would get stuck in this simple mock-up (where S is start and F is finish):
xxxxxxxxxx
Sooooooxxx
xxxxxxoxxx
xxxxxxFxxx
Your algorithm would proceed down the first hall until it faced the fall, turn left, be facing the "north" wall, turn left again, and walk back down the first hallway, where it would turn left twice again and keep repeating this problem.
The the right-hand rule algorithm (see the wikipedia page, as well as other sections for more maze algs) should do solve any maze without loops, and should be fairly easy to implement in java.
You could use a
Stack<Point> points = new Stack<>();
// add a point
Point p = new Point(x, y);
if (points.contains(p))
// been here before, in circles.
else
points.add(p);
For the algorithm portion, a depth first recursion through the stack is preferred. Something along the lines of:
currentSpot = (0,0) // The starting point //
while(! currentSpot.isExit()) {
if (! currentSpot.left().isWall()) stack.push(currentSpot.left());
if (! currentSpot.forward().isWall()) stack.push(currentSpot.forward());
if (! currentSpot.right().isWall()) stack.push(currentSpot.right());
currentSpot = stack.pop(); // Get the next location //
}
You'll want your point class to return the next point in each given direction (except backward), as well as detect when you'd be at the edges of the maze. You'll probably want a Maze class that contains all of the points, does the printing, stores the X/O, etc. So, you could probably replace the initial currentSpot = (0,0) with currentSpot = Maze.getStartingSpot();
For your algorithm, you do not need a stack. Only if you use backtracking to undo traversal decision, you would need a stack.
For the algorithm you could use backtracking (EDIT although it doesn't quite match your general idea.) You just have to realize your movements are "pushed" into a virtual stack, and they must be unpushed (and therefore undone.) You might have to implement the stack yourself if the "robot" is an actually moving object, but you can rely on the call stack if you just want to solve the maze.
Use wall follower algorithm: http://en.wikipedia.org/wiki/Maze_solving_algorithm#Wall_follower
We tackled this problem once when I was in school and we used a solution similar to the right/left hand rule. I believe we did this while learning about Linked Lists. In a nutshell, the algorithm was like this:
- Go left. If possible, repeat.
- If not, go straight. If possible, return to step 1.
- If not, go right. If possible, return to step 1.
At each step, you also check to see if the spot you are standing on is the finish. If you are unable to continue (ie, not able to go left, straight, or right), you then mark the spot you are standing on as 'visited' and back up. Rinse and repeat.
来源:https://stackoverflow.com/questions/9196514/using-a-stack-to-traverse-and-solve-a-maze-java