In: Computer Science
JAVA Program a solver for the Towers of Hanoi problem presented below. Submit the
Following: Hanoi.java, Driver.java (contains your main method).
Rules:
a) There are three Pillars (Pillar1, Pillar2, Pillar3).
b) There are N number of disks of increasing size (disk size is indicated by an integer).
c) At the start, all disks are stacked on one of the pillars.
d) At no point can a disk of larger size be placed above a disk of smaller size (including the start state).
e) You have to move all disks from the start pillar to a target pillar.
f) Only one disk can be moved at a time.
Sample Run:
Hanoi mySolver = new Hanoi(3,1,3);
//the first parameter is the number of disks
//the second parameter is the start pillar
//the third parameter is the target
The output should be:
My Solution is:
t0 Pillar1: 3 2 1
t0 Pillar2:
t0 Pillar3:
t1 Pillar1: 3 2
t1 Pillar2:
t1 Pillar3: 1
t2 Pillar1: 3
t2 Pillar2: 2
t2 Pillar3: 1
t3 Pillar1: 3
t3 Pillar2: 2 1
t3 Pillar3:
t4 Pillar1:
t4 Pillar2: 2 1
t4 Pillar3: 3
t5 Pillar1: 1
t5 Pillar2: 2
t5 Pillar3: 3
t6 Pillar1: 1
t6 Pillar2:
t6 Pillar3: 3 2
t7 Pillar1:
t7 Pillar2:
t7 Pillar3: 3 2 1
Find below the java code for the Towers of Hanoi problem.
it is asking for three parameters:
Java Code:
import java.util.Scanner;
import java.util.Stack;
class Hanoi {
public static int N;
private int start;
private int end;
private int num;
/* Creating Stack array */
public static Stack<Integer>[] tower = new Stack[4];
/* create to variable to get the number of steps required */
static int j = 0;
public Hanoi(int num, int start, int end) {
this.start = start;
this.end = end;
this.num = num;
tower[1] = new Stack<Integer>();
tower[2] = new Stack<Integer>();
tower[3] = new Stack<Integer>();
int mid = 2;
if (start == 1 && end == 2)
mid = 3;
else if (start == 2 && end == 3)
mid = 1;
else if (start == 3 && end == 1)
mid = 2;
else if (start == 1 && end == 3)
mid = 2;
else if (start == 2 && end == 1)
mid = 3;
else if (start == 3 && end == 2)
mid = 1;
N = num;
toh(num, start, mid, end);
}
/* Function to push disks into stack */
public static void toh(int n, int start, int mid, int end) {
for (int d = n; d > 0; d--)
tower[start].push(d);
display();
move(n, start, mid, end);
}
/* Recursive Function to move disks */
public static void move(int n, int a, int b, int c) {
if (n > 0) {
move(n - 1, a, c, b);
int d = tower[a].pop();
tower[c].push(d);
display();
move(n - 1, b, a, c);
}
}
/* Function to display */
public static void display() {
String D1 = " ", D2 = " ", D3 = " ";
for (int i = N - 1; i >= 0; i--) {
String d1 = " ", d2 = " ", d3 = " ";
try {
d1 = String.valueOf(tower[1].get(i));
} catch (Exception e) {
}
try {
d2 = String.valueOf(tower[2].get(i));
} catch (Exception e) {
}
try {
d3 = String.valueOf(tower[3].get(i));
} catch (Exception e) {
}
D1 += d1 + " ";
D2 += d2 + " ";
D3 += d3 + " ";
}
System.out.println("t" + j + " Pillar1:" + new StringBuilder(D1).reverse());
System.out.println("t" + j + " Pillar2:" + new StringBuilder(D2).reverse());
System.out.println("t" + j + " Pillar3:" + new StringBuilder(D3).reverse());
System.out.println("\n");
j++;
}
}
public class Driver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/* Accepting number of disks, start pillar and target pillar */
//the first parameter is the number of disks
System.out.print("Enter number of disks: ");
int num = scan.nextInt();
//the second parameter is the start pillar
System.out.print("Enter Start pillar: ");
int start = scan.nextInt();
//the third parameter is the target
System.out.print("Enter end pillar: ");
int end = scan.nextInt();
Hanoi mySolver = new Hanoi(num,start,end);
}
}
Output:
Enter number of disks: 3
Enter Start pillar: 1
Enter end pillar: 3
t0 Pillar1: 3 2 1
t0 Pillar2:
t0 Pillar3:
t1 Pillar1: 3 2
t1 Pillar2:
t1 Pillar3: 1
t2 Pillar1: 3
t2 Pillar2: 2
t2 Pillar3: 1
t3 Pillar1: 3
t3 Pillar2: 2 1
t3 Pillar3:
t4 Pillar1:
t4 Pillar2: 2 1
t4 Pillar3: 3
t5 Pillar1: 1
t5 Pillar2: 2
t5 Pillar3: 3
t6 Pillar1: 1
t6 Pillar2:
t6 Pillar3: 3 2
t7 Pillar1:
t7 Pillar2:
t7 Pillar3: 3 2 1