/* This is the skeleton of a program that finds the path out of a maze in 3 possible ways: recursively, with a stack, and with a queue. This problem illustrates searching using stacks (depth-first search) and queues (breadth-first search), and recursively. Laura Toma csci 210 */ import java.util.Stack; import java.util.LinkedList; public class Maze { final static char C=' ', X='x', S='s', E='e', V='.', P='*'; //the maze private static char[][] maze = { {X, X, X, X, X, X, X, X, X, X}, {X, S, C, C, C, C, C, C, C, X}, {X, C, C, C, X, C, X, X, C, E}, {X, C, X, X, X, C, X, X, C, X}, {X, C, C, C, C, X, X, X, C, X}, {X, X, X, X, C, X, X, X, C, X}, {X, X, X, X, C, X, C, C, C, X}, {X, X, C, X, C, X, X, C, C, X}, {X, X, C, C, C, C, C, C, C, X}, {X, X, X, X, X, X, C, X, X, X} }; //the start position final static int START_I = 1, START_J = 1; //the end position final static int END_I = 2, END_J = 9; //the path from start to end String path = ""; public static void main(String[] args) { Maze maze = new Maze(); maze.print(); //System.out.println("\n\nFind a path using a stack: "); //maze.solveStack(); //System.out.println("\n\nFind a path using a queue: "); //maze.solveQueue(); System.out.println("\n\nFind a path recursively: "); maze.solveRec(); } /* ****************************** */ //return the size of the maze (assume square) public int size() { return maze.length; } //print the maze public void print() { for (int i=0; i= 0 && i= 0 && j queue = new LinkedList(); //create the initial state and put it onto the queue MazePos ins = new MazePos(START_I, START_J, "START "); queue.add(ins); mark(ins); MazePos current = null; while (!queue.isEmpty()) { //take the first one in the queue current = queue.removeFirst(); if (isFinal(current)) break; //we found the solution //create all succesor of current and if valid add them to the queue //mark(current); //current should be marked assert(isMarked(current)); MazePos next; next = current.north(); if (isInMaze(next) && isClear(next)) { mark(next); queue.addLast(next); } next = current.south(); if (isInMaze(next) && isClear(next)) { mark(next); queue.addLast(next); } next = current.east(); if (isInMaze(next) && isClear(next)) { mark(next); queue.addLast(next); } next = current.west(); if (isInMaze(next) && isClear(next)) { mark(next); queue.addLast(next); } } //while if ((current!= null) && isFinal(current)) { System.out.println("solved using a QUEUE:"); print(); System.out.println("The path is: " + current.path()); } else { System.out.println("NOT solved using a QUEUE:"); } }//solvequeue //************************************************** //solve using recursion public void solveRec() { //call the recursive solver if (solve(new MazePos(START_I, START_J, "START "))) System.out.println("Got it: "); else System.out.println("You're stuck in the maze."); print(); System.out.println("The path is: " + path); } //find a path to exit the maze from this position. Works //recursively, by advancing to a neighbor and continuing from //there. Returns true if it finds a path, and false otherwise. public boolean solve(MazePos pos) { //if the current position is outside the maze, then there is //no solution from this position if (!isInMaze(pos)) return false; //if the current position is not clear, then there is no //solution from this position if (!isClear(pos)) return false; //is this the final state? if (isFinal(pos)) { // System.out.println("The path is: " + pos.path()); return true; } //if we are here, the current position is in the maze and it's clear mark(pos); //recurse MazePos next; next = pos.south(); if (solve(next)) { //the current point is on the path; mark it mark(pos, P); path = "south " + path; return true; } //try going north next = pos.north(); if (solve(next)) { //the current point is on the path; mark it mark(pos, P); path = "north " + path; return true; } //if we are here, going north did not find a solution next = pos.east(); if (solve(next)) { //the current point is on the path; mark it mark(pos, P); path = "east " + path; return true; } //if we are here, going north or east did not find a solution next = pos.west(); if (solve(next)) { //the current point is on the path; mark it path = "west " + path; mark(pos, P); return true; } //if we are here, can't go out through any of the neighbors, //so there is no solution return false; } }; class MazePos { int i, j; String path; public MazePos(int i, int j, String path) { this.i = i; this.j = j; this.path = path; }; public int i() { return i;} public int j() { return j;} public String path() {return path;} public void print() { System.out.println("(" + i + "," + j + ")"); } public MazePos north() { return new MazePos(i-1, j, path + " north "); } public MazePos south() { return new MazePos(i+1, j, path + " south "); } public MazePos east() { return new MazePos(i, j+1, path + " east "); } public MazePos west() { return new MazePos(i, j-1, path + " west "); } };