Questions tagged [towers-of-hanoi]

A simple puzzle involving moving discs between rods. Commonly used as a programming kata/exercise and frequently set as homework when introducing the concept of recursion.

The Tower of Hanoi or Towers of Hanoi , also called the Tower of Brahma or Towers of Brahma, is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.

The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:

  • Only one disk may be moved at a time.
  • Each move consists of taking the upper disk from one of the rods and sliding it onto another
  • rod, on top of the other disks that may already be present on that rod.
  • No disk may be placed on top of a smaller disk.
310 questions
2
votes
1 answer

Why does my recursive function with promises only wait once?

I'm trying to visualize the "Tower of Hanoi"-problem and tried using promises to make the functions wait for one disc's movement to be animated (which I simulated with setTimeout) before continuing to solve the problem. This test-code calculates the…
Bonsai
  • 23
  • 6
2
votes
0 answers

Formatting 2D Output for Towers of Hanoi in C

I have written a code to solve the Towers of Hanoi puzzle, but need help formatting an output to print a 2d representation of each move. I know I need to use stacks to solve this problem, but I am not sure how to do so. Can anyone help me come up…
NJF
  • 21
  • 2
2
votes
1 answer

Hanoi Towers in MIPS Assembly

I would like to implement Hanoi Towers algorithm in MIPS assembly. Rods are indicated by A, B and C. Input is number of number of disks and output is the sequence of moves necessary to solve the problem. For example: If input is 3, output should…
Itay4
  • 231
  • 1
  • 6
  • 14
2
votes
3 answers

Towers of Hanoi non-recursive function

I'm trying to figure out how to implement a non-recursive algorithm for the Hanoi Towers problem in function hanoi_2 below, but I have no idea how to continue... It throws an error: "can't pop from empty list". It works somehow when I input an odd…
2
votes
2 answers

Extended Hanoi Tower

Problem Definition I'm trying write a c++ program to solve the Extended Hanoi Tower problem. Extended Hanoi Tower is similar to the standard hanoi problem. The difference is that, odd rings are in A (1, 3, 5, ...) and even rings are in B (2, 4, 6,…
HoseinGhanbari
  • 1,058
  • 1
  • 11
  • 23
2
votes
1 answer

Hanoi assembly code from tanenbaum computer architecture 6th book

I'm trying to run the code presented in tanenbaum computer architecture 6th book. I typed exactly the same code as the book. here is the code: .686 .MODEL FLAT PUBLIC _towers EXTERN _printf:NEAR .CODE _towers: push ebp mov ebp, esp …
Kasra GH
  • 157
  • 2
  • 5
  • 22
2
votes
1 answer

Towers of Hanoi understanding

I have seen this code on stack overflow post. I didn't understand what is the function of this line int Other = 3 - Startpeg - Endpeg; Can anyone explain //Pegs are counted 0 to 2 void Tower(int Disk, int Startpeg, int Endpeg) { if(Disc <= 0) …
Muhammad Hamza
  • 213
  • 1
  • 4
  • 14
2
votes
4 answers

Tower of Hanoi displaying output. How to display 1 tab for 1st recursive call, 2 tabs for 2nd recursive call, etc?

My task was to add a bunch of print statements to display the the complete output of Tower of Hanoi to see and understand what it is doing behind the scenes, instead of just giving you the final result. class TowersApp { static int nDisks = 3; …
Sahat Yalkabov
  • 32,654
  • 43
  • 110
  • 175
2
votes
1 answer

Python: translating a printing recursive function into a generator

I have found this function: def hanoi(pegs, start, target, n): assert len(pegs[start]) >= n, 'not enough disks on peg' if n == 1: pegs[target].append(pegs[start].pop()) print '%i -> %i: %s' % (start, target, pegs) else: …
Caridorc
  • 6,222
  • 2
  • 31
  • 46
2
votes
2 answers

tower of hanoi prolog

Hello i have a problem with my implementation of the tower of hanoi. I need to print a list of list with the necessary moves, but my algorithm just work when the numbers of discs is N=1. This is my code move(1,X,Y,_,L) :- …
2
votes
1 answer

KeyPressed() not responding (Regular Java)

I am programming a Tower of Hanoi program as per the specifications of instructions given in class. Before proceeding, I would like to state that I have searched for similar answers and understand it is preferable to use KeyBinding. We are required…
AeroFighter76
  • 73
  • 1
  • 9
2
votes
2 answers

Solving Towers of Hanoi in C# using recursion

I'm facing the Towers of Hanoi problem, I read the concept and the recursive way of solving it from wikipedia, but I can not see what I'm missing in the implementation of the steps mentioned in wikipedia. I have seen many examples here but I don't…
Anon Dev
  • 1,361
  • 3
  • 14
  • 29
2
votes
2 answers

Printing the Tower of Hanoi Linearly

I am having trouble printing the tower of hanoi in C. My guess is that the cause of this is due to the algorithm is being run recursively. and the variables pegSource, pegSpare, and pegDest are getting switched up. causing it to print improperly. My…
Iceandele
  • 630
  • 1
  • 8
  • 25
2
votes
1 answer

C++ - Not understanding flow of recursive function

I'm a Computer Engineering student just now taking data structures. Today's lecture covered recursive functions as an easy way to solve factorial problems or sorting problems. My professor used the Towers of Hanoi example, and I really want to make…
2
votes
2 answers

Towers of Hanoi with two auxilary rods

I came across this problem on one of the websites. Requirement: Need to propose the algorithm and caclulate the time complexity of Towers of Hanoi problem if two auxilary rods are given instead of one. My Attempt: Original Towers of Hanoi: T(N)=…
Maheedhar Pamuru
  • 139
  • 1
  • 1
  • 7