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
-1
votes
1 answer

Best search algorithm for solving Towers of Hanoi?

Which search algorithm is most appropriate for solving the Towers of Hanoi? More specifically, of the uninformed search algorithms breadth-first search, depth-first search, and iterative deepening, which would be best?
-1
votes
1 answer

Is there a way to incrementally increase count in a recursion?

Is there a way to incrementally increase the count for the Tower of Hanoi challenge? At the moment, the count isn't incrementally increasing. Interestingly, it starts at five and hops around. See the output below. I tried adding a let count = 0…
peyo
  • 351
  • 4
  • 15
-1
votes
1 answer

Algorithm to insert 1 piece into imperfect Tower of Hanoi

I'm doing a program for my robot arm to build the tower of Hanoi. I got the algorithm working on a standing tower (replacing the tower to a different place using only 3 locations). Locations: A, B, C Pieces: 5 I'd like to build up the tower…
user6142080
-1
votes
1 answer

How can I make hardcoded stacks, not hardcoded and keep the program running properly

The Towers of Hanoi is very interesting and hard problem, which I assume we all know, but if someone does not here it is: Towers of Hanoi problem I have been searching and trying to implement a complete C++ solution, where by "complete" I mean that…
code_fan
  • 11
  • 2
-1
votes
2 answers

Function called multiple times with different parameters in recursion problem

I am fairly new to programming and I was just introduced to the concept of recursion. I've been presented with the famous Towers of Hanoi problem. Here is how the guy in the course I'm following has solved it like this: def printmove(fr,to): …
-1
votes
1 answer

Towers of Hanoi with recursion and vectors

I'm doing Towers of Hanoi for school, which will eventually take 7 discs and I cannot figure out how to use cout statements to display the moves that the discs make. For example, "Move disk 7 from peg1 to peg3." Any help on this would be much…
G.Egelstad
  • 13
  • 5
-1
votes
1 answer

Array of variable length array in JavaScript

I'm building small web page to solve the famous Hanoi towers problem. All is fine, however when trying to store state of three towers at each step (255 step for 8 disks) I tried to use array of array or array of objects that contain 3 arrays…
Ahmad Issa
  • 313
  • 1
  • 2
  • 15
-1
votes
1 answer

Towers of Hanoi C++ Implementation with Stacks

I've written the following code for a recursive ToH algorithm with stacks and can't figure out why it fails. There are no compilation errors, but once the program actually starts to run, it "thinks" a little bit then crashes. Any ideas? Relevant…
-1
votes
1 answer

Counting the minimum number of moves in Tower of Hanoi using recursion in java

I've been working on a method that returns the minimum number of moves that can be made in the Tower of Hanoi game complacently based on the number of rings. So far I have the basic code that can calculate 2^n (n being the number of rings). However,…
-1
votes
3 answers

How to run a method from a different class - JAVA

I am trying to call the toh method from my main class(Driver). When I make the call it gives me a null pointer exception. How can I call the toh method in Hanoi from the driver class? When I combine the classes into one it works fine but I need them…
Art
  • 29
  • 6
-1
votes
1 answer

binary representation of all states in tower of hanoi

In a slightly modified TOH we have 4 pegs. So in turn we have total of 4^N disk positions. Now in one of the solutions which I was going through, a given state is represented using below code - for(int disc = 0; disc < N; disc++){ state +=…
G.D
  • 305
  • 5
  • 18
-1
votes
2 answers

twist to tower of Hanoi of moving disks +1 or -1 only

I could replicate tower of Hanoi problem easily but here is a twist of moving disks +1 or -1 peg/tower only i.e. from peg/tower 1 it can go to space 2 only not to 3. so far I have this recursive working code for classic situation and struggling to…
Pradi KL
  • 696
  • 7
  • 13
-1
votes
1 answer

Segmentation fault in stack in push Function C language

I had faced problem with it when i start the program it crashed at function Full and push line 70 and 78 (Linux). I try to fix it but I always crash at the same place. #include #define SIZE 64 struct stack{ int TowerTop; …
Matthew Darens
  • 129
  • 2
  • 8
-1
votes
1 answer

Towers of Hanoi recursive

I've been trying to create a program to solve the towers of hanoi using recursive thinking. I've looked online a lot and cannot figure out why the code keeps printing out the wrong thing. Here is what I have so far: public static void tower(int…
J Doe
  • 11
  • 1
-1
votes
1 answer

Why isn't this Pascal program working?

This program needs to solve the towers of hanoi problem, but for some reason it won't work, here's my code. program haanoi ; procedure Hanoi(n: integer; A, B, C: char); begin if n = 1 then writeln(A, '-->', C) else …
SMALLname
  • 81
  • 2
  • 9