Question

In the classic problem of the Towers of Hanoi, you have 3 rods and N disks...

In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one).You have the following constraints:

(A) Only one disk can be moved at a time.

(B) A disk is slid off the top of one rod onto the next rod.

(C) A disk can only be placed on top of a larger disk.

Write a java program to move the disks from the first rod to the last using Stacks.

#with comments please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

if you have any doubts comment below.

Tower of Hanoi in java using stacks.

import java.util.*;

/* Class TowerOfHanoiUsingStacks */

public class TowerOfHanoiUsingStacks

{

public static int N;

/* Creating Stack array */

public static Stack<Integer>[] tower = new Stack[4];

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

tower[1] = new Stack<Integer>();

tower[2] = new Stack<Integer>();

tower[3] = new Stack<Integer>();

/* Accepting number of disks */   

System.out.println("Enter number of disks");

int num = scan.nextInt();

N = num;

toh(num);

}

/* Function to push disks into stack */

public static void toh(int n)

{

for (int d = n; d > 0; d--)

tower[1].push(d);

display();

move(n, 1, 2, 3);   

}

/* Recursive Function to move disks */

public static void move(int n, int a, int b, int c)

{

if (n > 0)

{

move(n-1, a, c, b);   

int d = tower[a].pop();   

tower[c].push(d);

display();   

move(n-1, b, a, c);   

}   

}

/* Function to display */

public static void display()

{

System.out.println(" A | B | C");

System.out.println("---------------");

for(int i = N - 1; i >= 0; i--)

{

String d1 = " ", d2 = " ", d3 = " ";

try

{

d1 = String.valueOf(tower[1].get(i));

}

catch (Exception e){

}

try

{

d2 = String.valueOf(tower[2].get(i));

}

catch(Exception e){

}

try

{

d3 = String.valueOf(tower[3].get(i));

}

catch (Exception e){

}

System.out.println(" "+d1+" | "+d2+" | "+d3);

}

System.out.println("\n");   

}

}

Add a comment
Know the answer?
Add Answer to:
In the classic problem of the Towers of Hanoi, you have 3 rods and N disks...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Describe a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary n (see...

    Describe a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary n (see Creativity Exercise C-5.16 for more details). C-5.16 In the Towers of Hanoi puzzle, we are given a platform with three pegs, a, b, and c, sticking out of it. On peg a is a stack of n disks, each larger than the next, so that the smallest is on the top and the largest is on the bottom. The puzzle is to move all...

  • Write the recursive MIPS code (with abundant explanatory comments) for the Tower of Hanoi problem of...

    Write the recursive MIPS code (with abundant explanatory comments) for the Tower of Hanoi problem of transferring a stack of N disks (smaller sized disks stacked over the larger sized ones) from a source peg to a destination peg via a third (temporary rest peg) under the constraints: 1. Only one disk is moved at a time from one peg to another 2. At no time, a larger disk will sit on a smaller one.

  • Please write a recursive Java program to solve the Tower of Hanoi game for n disks...

    Please write a recursive Java program to solve the Tower of Hanoi game for n disks on pole A. Please read the textbook page 176 – 180 to fully understand this game or puzzle. The game consists of n disks and three poles: A (the source), B (the destination), and C (the spare). Initially, all the disks are on pole A. The game is to move all disks (one by one) from pole A to pole B using pole C...

  • Towers of Hanoi (15 points) Given: n disks, all of different sizes the size of the...

    Towers of Hanoi (15 points) Given: n disks, all of different sizes the size of the ith disk is į .3 pegs - A, B, C the number of pegs might change in a future version of the game Inally, all the disks are stacked on peg A Requirement: Never place a larger disk on top of a smaller disk (disk 5 is larger than disk 4, etc.) Objective: Move the disks from peg A to peg C Input: n,...

  • Part A [50] in c++ A toy that many children play with is a base with...

    Part A [50] in c++ A toy that many children play with is a base with three pegs and five disks of different diameters. The disks begin on one peg, with the largest disk on the bottom and the other four disks added on in order of size. The idea is to move the disks from the peg they are on to another peg by moving only one disk at a time and without ever putting a larger disk on...

  • write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write...

    write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write a warruse function fibonacci(n) that calculatus 0, 1, 1, 2, 3, 5, 8, 13, 21,... (n) that calculates the n" Fib begins with the terms and 1 and has the property preceding terms. a) Write a cand unsigned long long int for i number. Use unsigned int for the function's paramete her that can be printed on your system. type. b) Determine the largest...

  • Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic me...

    Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic memory allocation, pointers, recursion, and debugging Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. Your solution will involve designing two classes one to represent individual Disk and another to represent the TowersOfHanoi game. TowersOfHanoi class will implement the game with three linked lists representing disks on each...

  • def count_overlapping_disks(disks): Right on the heels of the previous Manhattan skyline problem, another classic problem of...

    def count_overlapping_disks(disks): Right on the heels of the previous Manhattan skyline problem, another classic problem of similar spirit that is here best solved with a sweepline algorithm. Given a list of disks on the two-dimensional plane as tuples (x, y, r) so that (x, y) is the center point and r is the radius of that disk, count how many pairs of disks intersect each other in that their areas, including the edge, have at least one point in common....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT