//******************************************************************** // PalindromeTesters.java // // Demonstrates the same problem being solved through recursive // method and through an iterative method. //******************************************************************** import java.util.*; public class PalindromeTesters { public static boolean iterativeTester (String str) { boolean result = false; int left = 0; int right = str.length() - 1; while (left < right && str.charAt(left) == str.charAt(right)) { left++; right--; } if (left >= right) result = true; return result; } public static boolean recursiveTester (String str) { boolean result = false; if (str.length() <= 1) result = true; else result = (str.charAt(0) == str.charAt(str.length() - 1)) && recursiveTester(str.substring(1,str.length()-1)); return result; } } //******************************************************************** // Maze.java Author: Lewis and Loftus // // Represents a maze of characters. The goal is to get from the // top left corner to the bottom right, following a path of 1s. //******************************************************************** public class Maze { private final int TRIED = 3; private final int PATH = 7; private int[][] grid = { {1,1,1,0,1,1,0,0,0,1,1,1,1}, {1,0,1,1,1,0,1,1,1,1,0,0,1}, {0,0,0,0,1,0,1,0,1,0,1,0,0}, {1,1,1,0,1,1,1,0,1,0,1,1,1}, {1,0,1,0,0,0,0,1,1,1,0,0,1}, {1,0,1,1,1,1,1,1,0,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1,1,1,1,1,1,1} }; //----------------------------------------------------------------- // Attempts to recursively traverse the maze. It inserts special // characters indicating locations that have been tried and that // eventually become part of the solution. //----------------------------------------------------------------- public boolean traverse (int row, int column) { boolean done = false; if (valid (row, column)) { grid[row][column] = TRIED; // this cell has been tried if (row == grid.length-1 && column == grid[0].length-1) done = true; // the maze is solved else { done = traverse (row+1, column); // down if (!done) done = traverse (row, column+1); // right if (!done) done = traverse (row-1, column); // up if (!done) done = traverse (row, column-1); // left } if (done) // this location is part of the final path grid[row][column] = PATH; } return done; } //----------------------------------------------------------------- // Determines if a specific location is valid. //----------------------------------------------------------------- private boolean valid (int row, int column) { boolean result = false; // check if cell is in the bounds of the matrix if (row >= 0 && row < grid.length && column >= 0 && column < grid[row].length) // check if cell is not blocked and not previously tried if (grid[row][column] == 1) result = true; return result; } //----------------------------------------------------------------- // Returns the maze as a string. //----------------------------------------------------------------- public String toString () { String result = "\n"; for (int row=0; row < grid.length; row++) { for (int column=0; column < grid[row].length; column++) result += grid[row][column] + ""; result += "\n"; } return result; } } //******************************************************************** // MazeSearch.java Author: Lewis and Loftus // // Demonstrates recursion. //******************************************************************** public class MazeSearch { //----------------------------------------------------------------- // Creates a new maze, prints its original form, attempts to // solve it, and prints out its final form. //----------------------------------------------------------------- public static void main (String[] args) { Maze labyrinth = new Maze(); System.out.println (labyrinth); if (labyrinth.traverse (0, 0)) System.out.println ("The maze was successfully traversed!"); else System.out.println ("There is no possible path."); System.out.println (labyrinth); } } //******************************************************************** // SolveTowers.java Author: Lewis and Loftus // // Demonstrates recursion. //******************************************************************** public class SolveTowers { //----------------------------------------------------------------- // Creates a TowersOfHanoi puzzle and solves it. //----------------------------------------------------------------- public static void main (String[] args) { TowersOfHanoi towers = new TowersOfHanoi (4); towers.solve(); } } //******************************************************************** // TowersOfHanoi.java Author: Lewis and Loftus // // Represents the classic Towers of Hanoi puzzle. //******************************************************************** public class TowersOfHanoi { private int totalDisks; //----------------------------------------------------------------- // Sets up the puzzle with the specified number of disks. //----------------------------------------------------------------- public TowersOfHanoi (int disks) { totalDisks = disks; } //----------------------------------------------------------------- // Performs the initial call to moveTower to solve the puzzle. //----------------------------------------------------------------- public void solve () { moveTower (totalDisks, 1, 3, 2); } //----------------------------------------------------------------- // Moves the specified number of disks from one tower to another // by moving a subtower of n-1 disks out of the way, moving one // disk, then moving the subtower back. Base case of 1 disk. //----------------------------------------------------------------- private void moveTower (int numDisks, int start, int end, int temp) { if (numDisks == 1) moveOneDisk (start, end); else { moveTower (numDisks-1, start, temp, end); moveOneDisk (start, end); moveTower (numDisks-1, temp, end, start); } } //----------------------------------------------------------------- // Prints instructions to move one disk from the specified start // tower to the specified end tower. //----------------------------------------------------------------- private void moveOneDisk (int start, int end) { System.out.println ("Move one disk from " + start + " to " + end); } }