Tower of Hanoi – a sample c++ programs – algorithm.

Posted by Jiltin     29 March, 2009    5,523 views   

tower_of_hanoi_4Wikipedia gives “The Tower of Hanoi or Towers of Hanoi (also known as The 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 neatly stacked in 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.

Many recreational-math types are searching for the fastest algorithm or for ways to solve complex versions of the puzzle; it also is extensively utilized as an instructional study of recursive programming;

Here is the simple C program to solve:

/************************************************************

Program:Tower.c
Author :Jiltin
Date   :Feb 8,1994
Result :Successful

************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>

#define MAX  50
#define EMPTY 0
#define DONE (pos_matrix[0][disks-1]==pos_matrix[1][disks-1])

char names[3] = { ‘A’,‘B’,‘C’ };
int disks;
int pos_matrix[3][MAX];
int smallest_location = 0;
unsigned move_no = 0;

int main(int, char **);
void move_disk(int,int);
void itowers(int disks);
int next_disk(void);
char *g_time(void);
char *g_date(void);
char *time_diff(char *t1, char *t2);

int main(int argc, char *argv[])
{
int i,j;

char *run_date  ="        ";
char *start_time="        ";
char *finish_time  ="        ";
char *time_difference="           ";

if(argc!=2)
   {
   printf("Usage: %s [n] – where1 < n < %d\n",argv[0],MAX+1);
   return(1);
   }
disks=atoi(argv[1]);

if(disks < 2   disks > MAX)
   {
   printf("1 < disks < %d. Please try again. \n",MAX+1);
   return(1);
   }

printf("Solving Towers of Honoi\n");
printf("problem for %d disks   \n",disks);
printf("Using the iterative method… \n\n");

/* Initialize the towers and posts */
for(i=0; i < 3; i++)
   for(j=0; j < disks; j++)
      if(i==0)
         pos_matrix[i][j]=j+1;
      else
         pos_matrix[i][j]=EMPTY;

strcpy(run_date,g_date());
strcpy(start_time,g_time());
itowers(disks);
strcpy(finish_time,g_time());
time_difference=time_diff(finish_time,start_time);
printf("\nSolved in %u moves.\n",move_no);
printf("\nRun date    = %s",run_date);
printf("\nStart Time  = %s",start_time);
printf("\nFinish Time = %s",finish_time);
printf("\nTime taken  = %s",time_difference);
return(0);
}

void itowers(int disks)
{
int   direction,temp;

direction=( disks & 1 ) ? -1 : 1;

while(!DONE)
   {
   if(++move_no & 1)
      {
      temp = smallest_location;
      smallest_location  += direction;
      if(smallest_location < 0)
         smallest_location = 2;
      if(smallest_location > 2)
         smallest_location = 0;

      move_disk(temp,smallest_location);
      }
      else
      {
      temp=next_disk();
      move_disk(temp,3-temp-smallest_location);
      }
   }
}

int next_disk(void)
{
#define SIZE 0
#define POST 1

register location;
int post, avl[2][2],disk=0;

for(post=0;post< 3;post++)
   {
   if(post == smallest_location)
      continue;
   location=0;
   while((pos_matrix[post][location] == EMPTY) && (location < disks))
      location++;
   if(location == disks)
      continue;
   avl[disk][SIZE] = pos_matrix[post][location];
   avl[disk++][POST]=post;
   }
if(disk==1)
   return(avl[0][POST]);
return(avl[0][SIZE] < avl[1][SIZE] ? avl[0][POST] : avl[1][POST]);
}

void move_disk(int src, int dst)
{
register i=0,j=0;

while(pos_matrix[src][i] == EMPTY)
   i++;
while(pos_matrix[dst][j]==EMPTY && j < disks)
   j++;
j–;
printf("Move disk %d from   %c to %c. \n",pos_matrix[src][i],names[src],names[dst]);
pos_matrix[dst][j]=pos_matrix[src][i];
pos_matrix[src][i]=EMPTY;
}

char *g_time()
{
struct time st;
char *sys_time="               ";
   gettime(&st);
   sprintf(sys_time,"%2d:%2d:%2d",(int)st.ti_hour,(int)st.ti_min,(int)st.ti_sec);
   return(sys_time);
}

char *g_date()
{
struct date dt;
char *sys_date="               ";
   getdate(&dt);
   sprintf(sys_date,"%2d-%2d-%-2d",(int)dt.da_day,(int)dt.da_mon,((int)dt.da_year-1900));
   return(sys_date);
}

char *time_diff(char *t1, char *t2)
{
int h1,h2,m1,m2,s1,s2;
char temp[3]="  ";
int f1,f2,df;
char *diff_time="               ";
     temp[0]=t1[0]; temp[1]=t1[1]; temp[2]=\0; h1=atoi(temp);
     temp[0]=t2[0]; temp[1]=t2[1]; temp[2]=\0; h2=atoi(temp);
     temp[0]=t1[3]; temp[1]=t1[4]; temp[2]=\0; m1=atoi(temp);
     temp[0]=t2[3]; temp[1]=t2[4]; temp[2]=\0; m2=atoi(temp);
     temp[0]=t1[6]; temp[1]=t1[7]; temp[2]=\0; s1=atoi(temp);
     temp[0]=t2[6]; temp[1]=t2[7]; temp[2]=\0; s2=atoi(temp);
     f1=h1*3600+m1*60+s1; f2=h2*3600+m2*60+s2;
     df=f1-f2;
     h1=df/3600; m1=df/60 – h1*60; s1 = df – m1*60 – h1*3600;
     sprintf(diff_time,"%2d:%2d:%2d",h1,m1,s1);
     return(diff_time);
}

Following Google Searches Lead To This Post: write a c prog for tower of hanoi
iterative towers of hanoi in C
stack menara hanoi
program menara hanoi di c++
hanoi tower algorithm with stacks
make tower hanoi with C++
towers of hanoi iterative C
algorithm for tower of hanoi iterative method
tower of hanoi c++
“tower of hanoi algorithm” +”iterative”
towers of hanoi c++
tower of hanoi using stack C
code of iterative algorithm in Tower of hanoi
c# towers of hanoi
c++ hanoi disk
towers of hanoi algorithms and source code c++
tower of hanoi code in c++ iteratively
hanoi tower algorithm stacks
Write the recursive program for Tower of Hanoi problem.
Write the program for Tower of Hanoi problem in c++
tower of hanoi source code in c having at least 5 towers
c code for tower of hanoi iteratively
c# towers of hanoi iterative
tower of hanoi simple code by vb.net
iterative code Tower of Hanoi
6. Write a recursive program to solve Towers of Hanoi problem
iterative tower of hanoi code
towers hanoi c program
write a recursive program to solve Towers of Hanoi problem
iterativie method code of tower of hanoi
recursive program to solve Towers of Hanoi
hanoi game algorithm with stack
iterative tower of hanoi in c++ with stack
algorithm tower game
tower of hanoi using stack in c++
tower of hanoi algorithm explanation
C++ program in Towers Hanoi – explain
sliding puzzle algorithm recursive c++
making program to solve for towers of hanoi
hanoi mit c#
towers of hanoi iterative
hanoi tower c++
tower of brahma c++
recursive hanoi c++
Towers of Hanoi in c language
tower of hanoi programming in c++ with stack
write a c program about hanoi tower
write a recursive program to solve towers of Hanoi problem.
tower hanoi iterative simple
c++ iterative algorithm example
sample c algorithm
Write a recursive program to solve Towers of Hanoi problem.
towers of hanoi game for fedora
turnurile din hanoi in c# source
“c program to make hanoi game”
towers of hanoi algorithm stack
algorithm for tower of hanoi in c
code for Iterative solver towers of hanoi
code for Iterative solve towers of hanoi
tower hanoi iterative
c-Programmierung sys_time
tower of hanoi program using 5 towers
hanoi tower c++ source stack
tower of hanoi (c-programming)
towers of hanoi algorithms wiki
c++ hanoi tower solution
“tower of hanoi using stack”
sample code:tower of hannoi recursively in c++ using stack
program for tower of hanoi in c++
C++ Tower Of Hanoi
recursive program to solve Towers of Hanoi problem
Menara Hanoi C++
hanoi tower algorithm
example of iteration program using c
hanoi tower fastest algorithm
hanoi tower in c#
solve tower of hanoi with stack
algorithms for towers of hanoi wit stack
hanoi towers algorithm c++
tower of hanoi iteratively c++, stacks
C++ programs on algorithms
Towers of Hanoi iterative C++
how to make tower in c programing
hanoi tower php source
hanoi algorithm stack
hanoi program in c++
code algorithms hanoi tower in c++
iterative tower of hanoi c code
6. Write a recursive program to solve Towers of Hanoi problem.
itteration using C++
tower of hanoi program in c++ for linux
tower of hanoi c++ with stack
tower of hanoi problem solving using iterative stack
write a program of tower of hanoi in c++
turnurile di hanoi 4 tije
iterative algorithm for the tower of Hanoi
hanoi tower source code in c++
tower of hanoi recursive using iteration through stack
n tower of hanoi c++
hanoi towers fastest
hannoy tower + algorithm
write a program to implement towers of hanoi using c++
how to make tower of hanoy with c# 2008
towers of hannoi algorithm
tower of hanoi using stack in recursion,c++ code
simple coding of tower of hanoi using stack in c++
tower in c programming
6. Write a recursive program to solve Towers of Hanoi problem.in c
objective-c luhns algo
algorithm of tower of hanoi in c language
the tower of hanoi with 4 rods
tower of hanoi+iterative+stack
sys_time() c languaje programing
towers of hanoi c
c++ towers solver
towers of hanoi in c with stacks
hanoi towers c stack
the towers of hanoi game in language c
c++ menara hanoi
C language getdate yesterday
towers hanoi c#
C++ HANOI DISKS
+”iterative hanoi”
towers of hanoi c++ stack
how to do Towers of Hanoi in C# basic
arrays examples c++ towers of hanoi
towers of hanoi recursive c++
tower sample c#
c++ tower of hanoi example
towers of hanoi game recursive void
tower of hanoi c#
hanoi game using stacks c++
hanoi towers C++ .—
the hanoi towers using stacks in c
solving towers of hanoi recursive in C++
towers of hanoi program on c#
create tower of hanoi using vb.net
hanoi tower algorithm recursive
Tower of Hanoi in C program the towers
hanoi.c stacks
tower of hanoi c++ code
algoritma menara hanoi c
hanoi towers c++ with stack
hanoi iterative in c
Iterative Towers of Hanoi c++
C# Tower of hanoi Recursive
algorith for towers of hannoi problem
c cod for towers of HANOI using stack
Towers Of Hanoi C Program Using Arrays
site for c++ programs and algorithms
tower of hanoi c++ using stack
c program hanoi stacks
hanoi iterativer algorithmus example
how to make a hanoi game in c
Iterative method Hanoi c++
to make algorithm of c++ program
g_timer example C
c program hanoi
turnurile din hanoi c++
C++ hanoi example
explanation of algorithm of hanoi of tower
algorithm of Tower of Hanoi
hanoi tower c++ algorithm
c++ recursive towers of hanoi code
hanoi towers algorithm c
hanoi towers iterative algorithm
problem of hanoi in c++
tower of hanoi using stack
sample tower of hanoi c++
tower of hanoi stack
hanoi towers stacks c code
TowerofHanoi using stack c++
hanoi towers in c using stacks
hanoi with stack and recursion
c++ program for towers of brahma
c++ hanoi program
algorithms c++ code sample problem
hanoi wiki algorithm
hanoi c arrays
c programming hanoi tower
anoi problem in c code
hanoi towers c example
hanoi towers in c
“Hanoi algorithm”
towers of hanoi c++ stacks
tower of hanoi source code using stacks
hanoi c example
source code for tower of hanoi using c struct in recursion
turnurile din Hanoi C++
hanoi towers algorithm
coding of tower of hanoi using stack in c++
tower of hanoi solver in c
hanoi 3 array c
turnurile din hanoi-wikipedia
tower of hanoi recursive algorithm explanation
Hanoi towers solution on C#
hanoi towers in c using arrays
towers game c++
hanoi tower using array of stacks
tower of hanoi in stack algorithm
list hanoi in c
turnurile din hanoi in c++
hanoi towers in c programming
how to make algorithm of hanoi in c++?
hanoi tower c with stacks
iterations intower of hanoi
tower of hanoi using array
menara hanoi vb 6
tower of hanoi algorithm simple
game towers hanoi c++
Write a recursive program to solve Towers of Hanoi problem in c
program of Hanoi C++
towers of hanoi game ebay
using stack for hanoi tower algorithm
Hanoi algorithms
hanoi stack array
turnurile hanoi C++
tower hanoi iterative algorithm
GAME MENARA HANOI
towers of hanoi 4 rods
c program for tower of hanoi USING ITERATION
what is tower of hanoi c++
hanoi en c++ con arrays
c codes of the tower of hanoi
tower hanoi explanation c language
tower c++
WRITE A RECRUSIVE PROGRAM TO SOLVE THE TOWER OF HANOI PROBLEMS.
code script menara hanoi C
hanoi towers arrays C++
c programming stack towers of hanoi
struct hanoi in c
town of hanoi c++ source code
resolver hanoi tower algorithm
tower hanoi in php
hanoi tower shellscript
program menara hanoi
tower of hanoi in c#
c programming stack hanoi tower
“menara hanoi c++”
how to solve the tower of hanoi using stack of c
puzzle tower hanoi code
c++ example tower of hanoi
Hanoi Towers with 3 stacks C code
hanoi towers in c using struct
hanoi in c 2d
how to write algorithm for c programs

Post to Twitter  Post to Delicious  Post to Digg    Post to StumbleUpon

Categories : Source Code, Web & Scripts Tags :

Comments
November 20, 2009

I’m have this solution!!

#include
#include
/**
* author Fábio Moreira
from Brazil Fortaleza
email fabone.moreira@gmail.com
*/
int main(int argc, char *argv[])
{
int qtDiscos=6; //change the amount of disk!!!!!!!
int tamanhoMax=(int) pow(2, qtDiscos) – 1;
int sequenciaPares[3];//para pares
int indexPar=0;
int indexImpar=0;
int sequenciaImpares[3];//para impares
int i=0;
if (qtDiscos % 2 == 0) {
sequenciaPares[0]=12;
sequenciaPares[1]=23;
sequenciaPares[2]=31;
////////////////////////
sequenciaImpares[0]=13;
sequenciaImpares[1]=12;
sequenciaImpares[2]=32;
}else{
sequenciaPares[0]=13;
sequenciaPares[1]=32;
sequenciaPares[2]=21;
////////////////////////
sequenciaImpares[0]=12;
sequenciaImpares[1]=13;
sequenciaImpares[2]=23;
}
for (i = 0; i 2) {
indexPar = 0;
}
printf(” %d —> %d \n”,sequenciaPares[indexPar]/10,sequenciaPares[indexPar]%10);
indexPar++;
} else {
if (indexImpar > 2) {
indexImpar = 0;
}
printf(” %d —> %d \n”,sequenciaImpares[indexImpar]/10,sequenciaImpares[indexImpar]%10);
indexImpar++;
}
}

system(”PAUSE”);
return 0;
}

Posted by Fabio Moreira
November 20, 2009

its may solution!

Posted by Fabio Moreira
November 20, 2009

Sorry its my solution!

Posted by Fabio Moreira
Leave a comment

(required)

(required)