In: Computer Science
Write a java program to solve Towers of Hanoi with the condition that there are "m" number of rods and "n" number of disks. Where m >= 3 and n >=1.
public class Main
{
static void toh(int n ,char beg,char aux,char end)
{
if(n >= 1)
{
/* here beg is began rod end is end rod
and aux is auxiliary rod*/
toh(n-1,beg,end,aux);
// printing the movement of disk on the rods,
System.out.println(beg+" to "+end);
toh(n-1,aux,beg,end);
}
}
public static void main(String[] args)
{
char disk[] = {'A','B','C'};
int m = 3,n = disk.length;
if(m >= 3 && n >= 1)
{
toh(m,disk[0],disk[1],disk[2]);
}
}
}