public class TowerOfHanoi
{
public void solve(int n, String ST, String MID, String END)
{
if (n == 1)
{
System.out.println(ST + " -> " + END);
}
else
{
solve(n - 1, ST, END, MID);
System.out.println(ST + " -> " + END);
solve(n - 1, MID, ST, END);
}
}
public static void main(String[] args)
{
TowerOfHanoi obj = new TowerOfHanoi();
System.out.print("Enter number of discs: ");
Scanner scanner = new Scanner(System.in);
int discs = scanner.nextInt();
obj.solve(discs, "A", "B", "C");
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.