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
3 answers

Hanoi Towers, working but not supposed to

So we have the classical Hanoi problem, Im just getting into this recursion thing, and its a doozie! This is fully functioning, but I just don't understand how it could be! As I've understood it, for any n, it will print "from + " going to " +…
2
votes
4 answers

Towers of Hanoi with "counter" in python

I have written a code for "Towers of Hanoi" in python and I am trying to add a counter to show how many times it has run. I tried several things like a while loop and for loops etc. but it doesn't work. I am sure that the answer is pretty easy but…
Zahand
  • 490
  • 3
  • 12
  • 23
2
votes
1 answer

Towers of Hanoi in Scheme (recursive)

I wrote the following code in scheme today, but the evaluation is wrong. Please don't tell me I suck at programming, I understand that this is a classic recursion problem, but I am having trouble with it: (define (towers-of-hanoi n source temp…
John Friedrich
  • 343
  • 5
  • 21
2
votes
2 answers

Tail-Recursion for Towers of Hanoi in Scala

I'm new to programming in Scala. My aim is to implement a tail-recursive program for the Towers of Hanoi problem. I believe it can be achieved by recursion like this: // Implementing a recursive function for Towers of Hanoi,where the no of disks is…
Sammy
  • 21
  • 1
  • 2
2
votes
1 answer

Towers of Hanoi: Recursive Explanation in Ruby

NOTE: I understand the recursive solution; but I don't see how the code is achieving it, as I can't follow the step-by-step of the code's executing. I'm having a difficult time understanding the recursive loop for the Towers of Hanoi. I've commented…
Vendetta
  • 63
  • 1
  • 8
2
votes
3 answers

Binary solution for Tower of Hanoi

I am reading Algorithms by Robert Sedgewick Below is an excerpt from page 213, in reference to number of trailing zeros in binary representation of numbers. For the towers of Hanoi problem, the implication of the correspondence with n-bit numbers…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
2
votes
2 answers

How can I resolve towers of hanoi with breadth or depth first search, in Prolog?

in most implementations that I see, using a recursive solution. But, I don´t want this. I want search in tree with a search algorithm, like a breadth-fisrt or depth-first. thank you
2
votes
1 answer

Hanoi Tower(Towers of Hanoi)

I'm trying to do the Towers of Hanoi problem, what I have tried so far: move(1,[H|T],B,C,A1,B1,C) :- A1 = T, B1 = [H|B]. move(N,A,B,C,A1,B1,C) :- N>1, M is N-1, move(M,[H|T],C,B,A1,B1,C), move(1,[H|T],B,_,A1,B1,C),…
user1400451
  • 87
  • 1
  • 8
1
vote
3 answers

Counting function calls in the Towers of Hanoi?

So I want to count how many time the function moveSingleDisk() is called, but I can't seem to figure it out... Using this code: #include using namespace std; void moveTower(int n, char start, char finish, char temp, int count); void…
Jarrod
  • 1,655
  • 2
  • 21
  • 35
1
vote
1 answer

Solving recursive Towers of Hanoi in Lisp

My code in lisp is as follows: (defun solve-hanoi(from) (hanoi (length from) from '() '())) (defun hanoi(height from to aux) (when (>= height 1) (hanoi (- height 1) from aux to) (format t "~%Move ~a from…
kubasub
  • 455
  • 1
  • 5
  • 12
1
vote
0 answers

Towers of Hanoi ordering of functions?

Approached the towers of hanoi problem and was wondering why my output starts at "disk 1". Based on how the code is written to be (taken from geeksforgeeks website), shouldn't the expected output be: "Move disk 4 from rod A to rod C." and then once…
bamb1
  • 69
  • 5
1
vote
1 answer

Why doesn’t my solution to the Towers of Hanoi problem work correctly?

In the classic problem of the Towers of Hanoi, you have three towers 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 (i.e., each disk sits on top of an…
STACK_LIFO
  • 11
  • 3
1
vote
1 answer

Hanoi Puzzle With forbidden moves

I am trying to solve the Hanoi Puzzle, with forbidden moves. I have 3 poles, a left pole, a middle pole and a right pole. On the left pole I have 2 plates (the smallest is on the top and the biggest on the bottom) and want to get them to the right…
iTzTomy
  • 117
  • 6
1
vote
0 answers

Having trouble getting a Tower of Hanoi game to function correctly in Qualtrics

I'm trying to embed HTML/javascript/CSS code into a graphic/text question in Qualtrics to run a Tower of Hanoi game. Here's a link to a CodePen containing a functional example: https://codepen.io/finnhvman/pen/gzmMaa I'm able to input the HTML and…
1
vote
1 answer

Tower of Hanoi Python Iterative

Hi I am trying to create an iterative solution for Tower of Hanoi. However I cannot figure out how to incorporate the movement of the disk. For example in following function call for 2 disks, I want to return output as : Move disk 1 from A to…