# Reve's Puzzle in Java

Subject Computer Science Java Programming

## Question

Reve's puzzle is identical to the towers of Hanoi problem, except that there are 4 poles (instead of 3) The task is to move n discs of different sizes from the starting
pole to the destination pole, while obeying the following rules:
Move only one disc at a time.
Never place a larger disc on a smaller one.
The following remarkable algorithm, discovered by Frame and Stewart in 1941, transfers n discs from the starting pole to the destination pole using the fewest moves (although this fact was not proven until 2014).
Let k denote the integer nearest to n + 1 - √2n+1
Transfer (recursively) the k smallest discs to a single pole other than the start or destination poles.
Transfer the remaining n - k disks to the destination pole (without using the pole that now contains the smallest k discs). To do so, use the algorithm for the 3-pole towers of Hanoi problem
Transfer (recursively) the k smallest discs to the destination pole.
Write a program vesPuzzle java that takes an integer command-line argument n and prints a solution to Reve's puzzle Assume that the the discs are labeled in increasing order of size from 1 to n and that the poles are labeled A, B, C, and D, with A representing the starting pole and D representing the destination pole. Here are a few sample executions:
Recursive plan for Reve's Puzzle
/Desktop/recursion> java introcs RevesPuzzle 3
Move disc 1 from A to B
Move disc 2 from A to C
Move disc 3 from A to D
Move disc 2 from C to D
Move disc 1 from B to D

## Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

/**
* Recursive Tower of Hanoi (with 3 poles) method
* from --> mean start
* to   --> mean destination
*/
private static void hanoi(int n, int k, String from, String temp, String to) {
if (n == 0) {
System.out.println();
return;
}
hanoi(n - 1, k, from, to, temp);
System.out.print("Move disc " + (n + k) + " from " + from + " to " + to);
hanoi(n - 1, k, temp, from, to);
}

/**
* The 4 poles version.
*/
private static void rev(int n, String from, String temp1, String temp2, String to) {
if (n == 0) return;...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.