You are on page 1of 6

THE TOWERS OF HANOI

Artificial Inteligence

Alfonso Mrida Garca

Introduction
The Tower of Hanoi or Towers of Hanoi, also called the Tower of Brahma orTowers 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.

(From Wikipedia)

Recursive Algorithm
The disks are numbered from 1 to 8 ( 0 to n in general), starting with the smallest. The posts (which should be assumed aligned from left to right) are marked with capital letters (A, B and C). The initial goal will be A and C.

But the disc 1 ... 7 form a tower similar to the initial full well that in two of the three previous steps we face a similar problem to the original. In fact it can be solved in the same way, moving the towernow 1 ... 6. For example, the first step (move tower 1 ... 7 from A toB) can be decomposed into three steps.

Of course, in two of these three steps we meet again with the original problem, now with n=6. The process is not infinite, and it is time to move a tower that is equivalent to moving a single disc(this happens when the tower is a single disk, of course). In summary, the recursive algorithm is as follows: Algorithm to move the tower 1 ... n from the pole X to the pole Z using the poleY as auxiliary. If n = 1, Disc 1 takes X to Z and ends. Move the tower 1 ... n-1 using the same algorithm, from X to Y, using as auxiliary Z. Take the disk n from X to Z. Move the tower 1 ... n-1 using the same algorithm, Y to Z, using asauxiliary X.

SOURCE CODE:
#include "stdio.h" #include "unistd.h" void towers(int,char,char,char); void towers(int n,char fromtower,char totower,char auxtower) { /* If only 1 disk, make the move and return */ if(n==1) { printf("\nMove disk 1 from tower %c to tower %c",fromtower,totower); return; } /* Move top n-1 disks from X to Y, using C as auxiliary */ towers(n-1,fromtower,auxtower,totower); /* Move remaining disks from X to Z */ printf("\nMove disk %d from tower %c to tower %c",n,fromtower,totower); /* Move n-1 disks from Y to Z using X as auxiliary */ towers(n-1,auxtower,totower,fromtower); } main() { int n; printf("Enter the number of disks : "); scanf("%d",&n); printf("The Tower of Hanoi involves the moves :\n\n"); towers(n,'X','Z','Y'); while(1); return 0; }

Iterative Algorithm
In general, the disc moves in 2*n-k times between each movement and the next 2k-1 moves, and its first and last movement are separated 2k-1-1 moves from the beginning and end, respectively. This distribution of motion has a close relationship with the ones and zeros in the sequence of binary numbers. These are the first 32 numbers (0 to 31, all that can be written with five digits) in binary coded, sorted by columns.

At each step of a number to the next is exactly a 0 is changed by 1, and this 1 (marked in bold in the list) is always the first counting from the right. Both the first number (00000 do not have it in mind) as the lastending in 1 and between a number ending in 1 and the following is just another separating. These few marked in bold in the list indicate which disk is involved in every movement of the Tower ofHanoi. For example, returning to the tower of eight disks. Supposewe want to know which disk is moved in the movement 136. In binary and using 8 bits, 136 10001000 is written, so move disk 4,as the first bit set to a counting from right to left is the fourth. In c language, this will be:
for(i=x, j=1; ; i>>=1, j++) { if(i&1) break; }

It is also possible to determine directly from the binary form of m the post of origin and destination of the movement (rather than the hardto move and the direction of motion), resulting in a very short and fast algorithm.
i=x&x-1; fr=(i+i/3)&3; i=(x|x-1)+1; to=(i+i/3)&3;

From http://hanoitower.mkolar.org/shortestTHalgo.html

SOURCE CODE
int main() { int n, x; printf( "How many disks? " ); scanf( "%d", &n ); printf("\n"); int fr, to; unsigned int i, j; for(x=1; x < (1 << n); x++) { i=x&x-1; fr=(i+i/3)&3; i=(x|x-1)+1; to=(i+i/3)&3; for(i=x, j=1; ; i>>=1, j++) { if(i&1) break; } printf( "move disk %i from %i to %i\n", j, fr, to ); } return 0; }

You might also like