Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tower of Hanoi problem - linear planning

Tags:

java

I'm doing an assignment about planning for tower of Hanoi problem using linear planning and I'm not allowed to use any recursive functions. The matter is that my solution is not optimal like the one from recursive method. It produces redundant steps. For example:

I have 3 rods named A, B, C respectively and there are 2 disks named 1, 2 (disk 1 is smaller than disk 2, disk 1 is on disk 2), then there are 2 ways to move all the disks from rod A to rod C using rod B as intermediate rod as follow:

  1. (optimal like the output of recursive algorithm)
    • Move disk 1 to rod B
    • Move disk 2 to rod C
    • Move disk 1 to rod C
  2. (non optimal using planning)
    • Move disk 1 to rod C
    • Move disk 2 to rod B
    • Move disk 1 to rod A
    • Move disk 2 to rod C
    • Move disk 1 to rod C

So how do I (more precise: an algorithm which can be programmable) know that disk 1 must move to rod B first instead of moving to disk C to gain optimal solution? I'll really appreciate your help. Thanks!

like image 242
crazy_in_love Avatar asked Jun 04 '11 16:06

crazy_in_love


People also ask

What is the problem of Tower of Hanoi?

Initially, all the disks are placed on one rod, one over the other in ascending order of size similar to a cone-shaped tower. The objective of this problem is to move the stack of disks from the initial rod to another rod, following these rules: A disk cannot be placed on top of a smaller disk.

What is Tower of Hanoi formula?

Given a Colored Magnetic Tower of Hanoi, the number of moves of disk k are P(k) = 3(k-1) and the total number of moves is S(N) = (3N – 1)/2.

What are the applications of Tower of Hanoi problem?

Applications of Tower of Hanoi: It has been used to determine the extent of brain injuries and helps to build/rebuild neural pathways in the brain as attempting to solve, Tower of Hanoi uses parts of the brain that involve managing time, foresight of whether the next move will lead us closer to the solution or not.


2 Answers

I wanted to visualize ratchet freak answer.

Example of size 5

enter image description here

Example of size 6

enter image description here

Larger sizes work the same way.

Additional details

  • The task is to write Hamilton path algorithm implementation.
  • There are 3 rods{initial, spare, destination}. Your starting move depends on reminder of 2 :
    • if n % 2 == 0, start with spare rod
    • if n % 2 == 1, start with destination rod
  • It takes 2^n-1 moves, to move the whole stack to destination stack.
like image 152
Margus Avatar answered Sep 21 '22 02:09

Margus


the key is knowing how many disks need to be moved:

it there are a even number of disks in the stack you start to the "spare" rod
if there are an odd number of disks in the stack you start to the "destination" rod

like image 35
ratchet freak Avatar answered Sep 19 '22 02:09

ratchet freak