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...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner (using Stack) in C++(using data structure). Note: you have to solve for N number of disks, 3 Towers (Stacks). Do not use recursion. For better understanding play the game at least once. Link:https://www.mathsisfun.com/games/towerofhanoi.html
towers of hanoi c++ program using stacks and singly linked lists.
towers of hanoi c++ program using stacks and singly linked lists.
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...
How many moves are required to solve the Towers of Hanoi problem when we have 3...
How many moves are required to solve the Towers of Hanoi problem when we have 3 disks? Group of answer choices 5 6 8 4 7 After the following statements execute, what item is at the front of the queue? QueueInterface<String> zooDelivery = new LinkedQueue<>(); zooDelivery.enqueue("lion"); zooDelivery.enqueue("tiger"); zooDelivery.enqueue("cheetah"); String next = zooDelivery.dequeue(); next = zooDelivery.dequeue(); zooDelivery.enqueue("jaguar"); zooDelivery.enqueue("cat"); Group of answer choices "lion" "tiger" "cheetah" "jaguar" "cat" When a recursive method does not include a base case, calling the method results...
By using javaFX, write and design the Towers of Hanoi game with Redo and Undo features...
By using javaFX, write and design the Towers of Hanoi game with Redo and Undo features with play and solve options.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT