Question

In: Computer Science

Write a java program to solve Towers of Hanoi with the condition that there are "m"...

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.

Solutions

Expert Solution

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]);
}
}
}


Related Solutions

JAVA Program a solver for the Towers of Hanoi problem presented below. Submit the Following: Hanoi.java,...
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...
Describe an algorithm to solve the variant of the Towers of Hanoi in as few moves...
Describe an algorithm to solve the variant of the Towers of Hanoi in as few moves as possible. Prove that your algorithm is correct. Initially, all the n disks are on peg 1, and you need to move the disks to peg 2. You are not allowed to put a bigger disk on top of a smaller disk. 1. Suppose you are forbidden to move any disk directly between peg 1 and peg 2, and every move must involve (the...
Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas:...
Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas: Hanoi.java, Driver.java (contains your main method). This is an individual project. Students may discuss solutions to the problem but may not share code with one another. I will discuss this project during our next lecture. 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...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the following declaration: void towers(int n, char source, char dest, char spare); The function outputs a message describing each move. The source, destination, and spare poles are indicated with a character identifier of your choosing ('A', 'B', 'C' are common). Write a MIPS assembly language program that demonstrates the Towers of Hanoi procedure. Your program should ask the user for the number of disks. The...
There is a famous puzzle called the Towers of Hanoi that consists of three pegs and...
There is a famous puzzle called the Towers of Hanoi that consists of three pegs and n circular disks, all of different sizes. The disks start on the leftmost peg, with the largest disk on the bottom, the second largest on top of it, and so on, up to the smallest disk on top. The goal is to move the disks so that they are stacked in this same order on the rightmost peg. However, you are allowed to move...
Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm...
Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm to solve it in as few moves as possible. Prove that your algorithm is correct. Initially, all the n disks are on peg 1, and you need to move the disks to peg 2. In all the following variants, you are not allowed to put a bigger disk on top of a smaller disk. Consider the disappearing Tower of Hanoi puzzle where the largest...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there can be any amount of disks and there are either 3,4, or 5 pegs to store the disks(# of pegs and disks is manually inputted by user), in Tower of Hanoi game, the object of the game is move all the pieces from the 1st peg to the right most peg or the opposite side. You can place disks on top of each other...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT