Java - Tower Of Hanoi Algorithm Source Code
You can find the complete Java source code for Tower of Hanoi algorithm. Given the number of discs as input, you can get the print out of the list of steps you need to solve the problem.
This program is developed in Java application and takes the number of discs as input. It uses recursive logic to find the number of steps required to solve thr problem. Initially the discs are getting added into the stack. I have 3 stacks A, B and C for 3 towers A, B and C. The function SolveTOH is used to compute all possible steps to solve the problem. Note that it uses the recursive logic and the output after each move is written on the screen.
The Big O notation for this problem is: 2^n - 1.
The other related links are given below:
- Tower Of Hanoi Algorithm Source Code in Java
- Tower Of Hanoi Algorithm Source Code in C#
- Tower Of Hanoi Algorithm Source Code in Visual C++
- Tower Of Hanoi Algorithm Source Code in Turbo C++
- Tower Of Hanoi Algorithm Source Code in Turbo C
The complete Java source code and .class file can be found at the end of the page.
Source Code
import java.io.*;
import java.lang.*;
import java.util.*;
class TowerOfHanoi
{
static int movecount = 0;
static public void Solve2DiscsTOH(Stack source, Stack temp, Stack dest)
{
temp.push(source.pop());
movecount++;
PrintStacks();
dest.push(source.pop());
movecount++;
PrintStacks();
dest.push(temp.pop());
movecount++;
PrintStacks();
}
static public int SolveTOH(int nDiscs, Stack source, Stack temp, Stack dest)
{
if (nDiscs <= 4)
{
if ((nDiscs % 2) == 0)
{
Solve2DiscsTOH(source, temp, dest);
nDiscs = nDiscs - 1;
if (nDiscs == 1)
return 1;
temp.push(source.pop());
movecount++;
PrintStacks();
//new source is dest, new temp is source, new dest is temp;
Solve2DiscsTOH(dest, source, temp);
dest.push(source.pop());
movecount++;
PrintStacks();
//new source is temp, new temp is source, new dest is dest;
SolveTOH(nDiscs, temp, source, dest);
}
else
{
if (nDiscs == 1)
return -1;
Solve2DiscsTOH(source, dest, temp);
nDiscs = nDiscs - 1;
dest.push(source.pop());
movecount++;
PrintStacks();
Solve2DiscsTOH(temp, source, dest);
}
return 1;
}
else if (nDiscs >= 5)
{
SolveTOH(nDiscs - 2, source, temp, dest);
temp.push(source.pop());
movecount++;
PrintStacks();
SolveTOH(nDiscs - 2, dest, source, temp);
dest.push(source.pop());
movecount++;
PrintStacks();
SolveTOH(nDiscs - 1, temp, source, dest);
}
return 1;
}
static public Stack A = new Stack();
static public Stack B = new Stack();
static public Stack C = new Stack();
static public void PrintStacks()
{
if (countA != A.size() ||
countB != B.size() ||
countC != C.size())
{
int diffA = A.size() - countA;
int diffB = B.size() - countB;
int diffC = C.size() - countC;
if (diffA == 1)
{
if (diffB == -1)
System.out.print("Move Disc " + A.peek() + " From B To A");
else
System.out.print("Move Disc " + A.peek() + " From C To A");
}
else if (diffB == 1)
{
if (diffA == -1)
System.out.print("Move Disc " + B.peek() + " From A To B");
else
System.out.print("Move Disc " + B.peek() + " From C To B");
}
else //if (diffC == 1)
{
if (diffA == -1)
System.out.print("Move Disc " + C.peek() + " From A To C");
else
System.out.print("Move Disc " + C.peek() + " From B To C");
}
countA = A.size();
countB = B.size();
countC = C.size();
System.out.println();
}
PrintStack(A);
System.out.print(" , ");
PrintStack(B);
System.out.print(" , ");
PrintStack(C);
System.out.print(" , ");
}
static int countA = 0;
static int countB = 0;
static int countC = 0;
static public void PrintStack(Stack s)
{
System.out.print(s.toString());
}
public static void main(String[] args)
{
try
{
while (true)
{
System.out.print("\nEnter the number of discs (-1 to exit): ");
int maxdisc = 0;
String inpstring = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
inpstring = reader.readLine();
movecount = 0;
maxdisc = Integer.parseInt(inpstring);
if (maxdisc == -1)
{
System.out.println("Good Bye!");
return;
}
if (maxdisc <= 1 || maxdisc >= 10)
{
System.out.println("Enter between 2 - 9");
continue;
}
for (int i = maxdisc; i >= 1; i--)
A.push(i);
countA = A.size();
countB = B.size();
countC = C.size();
PrintStacks();
SolveTOH(maxdisc, A, B, C);
System.out.println("Total Moves = " + movecount);
while (C.size() > 0)
C.pop();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Click here to download the Java Source Code and executable
Output
D:\Program Files\Java\jdk1.6.0_25\bin>javac TowerOfHanoi.java
D:\Program Files\Java\jdk1.6.0_25\bin>java TowerOfHanoi
Enter the number of discs (-1 to exit): 0
Enter between 2 - 9
Enter the number of discs (-1 to exit): 1
Enter between 2 - 9
Enter the number of discs (-1 to exit): 2
[2, 1] , [] , [] , Move Disc 1 From A To B
[2] , [1] , [] , Move Disc 2 From A To C
[] , [1] , [2] , Move Disc 1 From B To C
[] , [] , [2, 1] , Total Moves = 3
Enter the number of discs (-1 to exit): 3
[3, 2, 1] , [] , [] , Move Disc 1 From A To C
[3, 2] , [] , [1] , Move Disc 2 From A To B
[3] , [2] , [1] , Move Disc 1 From C To B
[3] , [2, 1] , [] , Move Disc 3 From A To C
[] , [2, 1] , [3] , Move Disc 1 From B To A
[1] , [2] , [3] , Move Disc 2 From B To C
[1] , [] , [3, 2] , Move Disc 1 From A To C
[] , [] , [3, 2, 1] , Total Moves = 7
Enter the number of discs (-1 to exit): 4
[4, 3, 2, 1] , [] , [] , Move Disc 1 From A To B
[4, 3, 2] , [1] , [] , Move Disc 2 From A To C
[4, 3] , [1] , [2] , Move Disc 1 From B To C
[4, 3] , [] , [2, 1] , Move Disc 3 From A To B
[4] , [3] , [2, 1] , Move Disc 1 From C To A
[4, 1] , [3] , [2] , Move Disc 2 From C To B
[4, 1] , [3, 2] , [] , Move Disc 1 From A To B
[4] , [3, 2, 1] , [] , Move Disc 4 From A To C
[] , [3, 2, 1] , [4] , Move Disc 1 From B To C
[] , [3, 2] , [4, 1] , Move Disc 2 From B To A
[2] , [3] , [4, 1] , Move Disc 1 From C To A
[2, 1] , [3] , [4] , Move Disc 3 From B To C
[2, 1] , [] , [4, 3] , Move Disc 1 From A To B
[2] , [1] , [4, 3] , Move Disc 2 From A To C
[] , [1] , [4, 3, 2] , Move Disc 1 From B To C
[] , [] , [4, 3, 2, 1] , Total Moves = 15
Enter the number of discs (-1 to exit): 5
[5, 4, 3, 2, 1] , [] , [] , Move Disc 1 From A To C
[5, 4, 3, 2] , [] , [1] , Move Disc 2 From A To B
[5, 4, 3] , [2] , [1] , Move Disc 1 From C To B
[5, 4, 3] , [2, 1] , [] , Move Disc 3 From A To C
[5, 4] , [2, 1] , [3] , Move Disc 1 From B To A
[5, 4, 1] , [2] , [3] , Move Disc 2 From B To C
[5, 4, 1] , [] , [3, 2] , Move Disc 1 From A To C
[5, 4] , [] , [3, 2, 1] , Move Disc 4 From A To B
[5] , [4] , [3, 2, 1] , Move Disc 1 From C To B
[5] , [4, 1] , [3, 2] , Move Disc 2 From C To A
[5, 2] , [4, 1] , [3] , Move Disc 1 From B To A
[5, 2, 1] , [4] , [3] , Move Disc 3 From C To B
[5, 2, 1] , [4, 3] , [] , Move Disc 1 From A To C
[5, 2] , [4, 3] , [1] , Move Disc 2 From A To B
[5] , [4, 3, 2] , [1] , Move Disc 1 From C To B
[5] , [4, 3, 2, 1] , [] , Move Disc 5 From A To C
[] , [4, 3, 2, 1] , [5] , Move Disc 1 From B To A
[1] , [4, 3, 2] , [5] , Move Disc 2 From B To C
[1] , [4, 3] , [5, 2] , Move Disc 1 From A To C
[] , [4, 3] , [5, 2, 1] , Move Disc 3 From B To A
[3] , [4] , [5, 2, 1] , Move Disc 1 From C To B
[3] , [4, 1] , [5, 2] , Move Disc 2 From C To A
[3, 2] , [4, 1] , [5] , Move Disc 1 From B To A
[3, 2, 1] , [4] , [5] , Move Disc 4 From B To C
[3, 2, 1] , [] , [5, 4] , Move Disc 1 From A To C
[3, 2] , [] , [5, 4, 1] , Move Disc 2 From A To B
[3] , [2] , [5, 4, 1] , Move Disc 1 From C To B
[3] , [2, 1] , [5, 4] , Move Disc 3 From A To C
[] , [2, 1] , [5, 4, 3] , Move Disc 1 From B To A
[1] , [2] , [5, 4, 3] , Move Disc 2 From B To C
[1] , [] , [5, 4, 3, 2] , Move Disc 1 From A To C
[] , [] , [5, 4, 3, 2, 1] , Total Moves = 31
Enter the number of discs (-1 to exit): 6
[6, 5, 4, 3, 2, 1] , [] , [] , Move Disc 1 From A To B
[6, 5, 4, 3, 2] , [1] , [] , Move Disc 2 From A To C
[6, 5, 4, 3] , [1] , [2] , Move Disc 1 From B To C
[6, 5, 4, 3] , [] , [2, 1] , Move Disc 3 From A To B
[6, 5, 4] , [3] , [2, 1] , Move Disc 1 From C To A
[6, 5, 4, 1] , [3] , [2] , Move Disc 2 From C To B
[6, 5, 4, 1] , [3, 2] , [] , Move Disc 1 From A To B
[6, 5, 4] , [3, 2, 1] , [] , Move Disc 4 From A To C
[6, 5] , [3, 2, 1] , [4] , Move Disc 1 From B To C
[6, 5] , [3, 2] , [4, 1] , Move Disc 2 From B To A
[6, 5, 2] , [3] , [4, 1] , Move Disc 1 From C To A
[6, 5, 2, 1] , [3] , [4] , Move Disc 3 From B To C
[6, 5, 2, 1] , [] , [4, 3] , Move Disc 1 From A To B
[6, 5, 2] , [1] , [4, 3] , Move Disc 2 From A To C
[6, 5] , [1] , [4, 3, 2] , Move Disc 1 From B To C
[6, 5] , [] , [4, 3, 2, 1] , Move Disc 5 From A To B
[6] , [5] , [4, 3, 2, 1] , Move Disc 1 From C To A
[6, 1] , [5] , [4, 3, 2] , Move Disc 2 From C To B
[6, 1] , [5, 2] , [4, 3] , Move Disc 1 From A To B
[6] , [5, 2, 1] , [4, 3] , Move Disc 3 From C To A
[6, 3] , [5, 2, 1] , [4] , Move Disc 1 From B To C
[6, 3] , [5, 2] , [4, 1] , Move Disc 2 From B To A
[6, 3, 2] , [5] , [4, 1] , Move Disc 1 From C To A
[6, 3, 2, 1] , [5] , [4] , Move Disc 4 From C To B
[6, 3, 2, 1] , [5, 4] , [] , Move Disc 1 From A To B
[6, 3, 2] , [5, 4, 1] , [] , Move Disc 2 From A To C
[6, 3] , [5, 4, 1] , [2] , Move Disc 1 From B To C
[6, 3] , [5, 4] , [2, 1] , Move Disc 3 From A To B
[6] , [5, 4, 3] , [2, 1] , Move Disc 1 From C To A
[6, 1] , [5, 4, 3] , [2] , Move Disc 2 From C To B
[6, 1] , [5, 4, 3, 2] , [] , Move Disc 1 From A To B
[6] , [5, 4, 3, 2, 1] , [] , Move Disc 6 From A To C
[] , [5, 4, 3, 2, 1] , [6] , Move Disc 1 From B To C
[] , [5, 4, 3, 2] , [6, 1] , Move Disc 2 From B To A
[2] , [5, 4, 3] , [6, 1] , Move Disc 1 From C To A
[2, 1] , [5, 4, 3] , [6] , Move Disc 3 From B To C
[2, 1] , [5, 4] , [6, 3] , Move Disc 1 From A To B
[2] , [5, 4, 1] , [6, 3] , Move Disc 2 From A To C
[] , [5, 4, 1] , [6, 3, 2] , Move Disc 1 From B To C
[] , [5, 4] , [6, 3, 2, 1] , Move Disc 4 From B To A
[4] , [5] , [6, 3, 2, 1] , Move Disc 1 From C To A
[4, 1] , [5] , [6, 3, 2] , Move Disc 2 From C To B
[4, 1] , [5, 2] , [6, 3] , Move Disc 1 From A To B
[4] , [5, 2, 1] , [6, 3] , Move Disc 3 From C To A
[4, 3] , [5, 2, 1] , [6] , Move Disc 1 From B To C
[4, 3] , [5, 2] , [6, 1] , Move Disc 2 From B To A
[4, 3, 2] , [5] , [6, 1] , Move Disc 1 From C To A
[4, 3, 2, 1] , [5] , [6] , Move Disc 5 From B To C
[4, 3, 2, 1] , [] , [6, 5] , Move Disc 1 From A To B
[4, 3, 2] , [1] , [6, 5] , Move Disc 2 From A To C
[4, 3] , [1] , [6, 5, 2] , Move Disc 1 From B To C
[4, 3] , [] , [6, 5, 2, 1] , Move Disc 3 From A To B
[4] , [3] , [6, 5, 2, 1] , Move Disc 1 From C To A
[4, 1] , [3] , [6, 5, 2] , Move Disc 2 From C To B
[4, 1] , [3, 2] , [6, 5] , Move Disc 1 From A To B
[4] , [3, 2, 1] , [6, 5] , Move Disc 4 From A To C
[] , [3, 2, 1] , [6, 5, 4] , Move Disc 1 From B To C
[] , [3, 2] , [6, 5, 4, 1] , Move Disc 2 From B To A
[2] , [3] , [6, 5, 4, 1] , Move Disc 1 From C To A
[2, 1] , [3] , [6, 5, 4] , Move Disc 3 From B To C
[2, 1] , [] , [6, 5, 4, 3] , Move Disc 1 From A To B
[2] , [1] , [6, 5, 4, 3] , Move Disc 2 From A To C
[] , [1] , [6, 5, 4, 3, 2] , Move Disc 1 From B To C
[] , [] , [6, 5, 4, 3, 2, 1] , Total Moves = 63
Enter the number of discs (-1 to exit): 10
Enter between 2 - 9
Enter the number of discs (-1 to exit): -1
Good Bye!
|