//************************************************** //this solution uses a stack to keep track of possible //states/positions to explore; it marks the maze to remember the //positions that it's already explored. public void solveStack() { //create teh search queue Stack stack = new Stack(); //create initial state and put it in the queue MazePos ins = new MazePos(START_I, START_J); stack.push(ins); //save the maze char[][] mazecopy = this.clone(); MazePos crts = null; while (!stack.isEmpty()) { //get current state crts = stack.pop(); if (isFinal(crts)) break; //mark it mark(crts); //generate all possible states and enqueue them MazePos next; next = crts.north(); if (isInMaze(next) && isClear(next)) stack.push(next); next = crts.east(); if (isInMaze(next) && isClear(next)) stack.push(next); next = crts.south(); if (isInMaze(next) && isClear(next)) stack.push(next); next = crts.west(); if (isInMaze(next) && isClear(next)) stack.push(next); } //if you are here, either queue is empty or crts is final if (crts!= null && isFinal(crts)) { System.out.println("Got the solution"); //print the maze print(); } else { System.out.println("NO solution"); } //restore the maze restore(mazecopy); //create teh search queue }