#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[70]; //src disk array
int b[70];//temp disk array
int c[70];//tar disk array
int val, srcval=1,tempval=0,tarval=0;
int es[3] = {1,1,0}; //array to decide iteration of disks from and to
int et[3] = {1,0,1};
int etr[3]= {0,1,1};
int os[3] = {1,1,0};
int ot[3] = {0,1,1};
int otr[3]= {1,0,1};
void find();//to keep the update of elements on top(srcval,tempval,tarval)
void move(int[],int[]);//to move from src to tar
void main()
{
int i1 =0;
long int j1;
int *src;
int *temp;
int *tar;
printf("Enter The number of disks");
scanf("%d",&val);
if(val % 2 == 0) //even no of disk
{
src = es;
temp = et;
tar = etr;
}
else //odd no of disk
{
src = os;
temp = ot;
tar = otr;
}
for(j1 = 1;j1<=val;j1++)
{
a[j1-1]=j1;
b[j1-1]=0;
c[j1-1]=0;
}
for(j1 = 1;j1<=(long)(pow(2.0,val)-1);j1++)
{
getch();
if(src[i1]==1&&tar[i1]==1)
{
if(srcval > tarval )
{
if(tarval == 0)
{
printf("Source to target");
move(a,c);
}
else{
printf("target to source");
move(c,a); }
}
else
{
if(srcval==0){
printf("Target to source");
move(c,a);
}
else{
printf("source to target");
move(a,c);
}
}
}
if(src[i1]==1&&temp[i1]==1)
{
if(srcval>tempval)
{
if(tempval==0)
{
printf("source to temperory");
move(a,b);
}
else
{
move(b,a);
printf("temperory to source ");
}
}
else
{
if(srcval == 0)
{
move(b,a);
printf("temperory to source ");
}
else
{
move(a,b);
printf("source to temperory");
}
}
}
if(temp[i1]==1&&tar[i1]==1)
{
if(tempval>tarval )
{
if(tarval==0)
{
printf("temperory to target");
move(b,c);
}
else
{
printf("target to temperory");
move(c,b);
}
}
else
{
if(tempval==0)
{
printf("target to temperory");
move(c,b);
}
else
{
printf("temperory to target");
move(b,c);
}
}
}
if(i1<2)
i1++;
else
i1=0;
// getch();
printf("\n");
}
}
void find()//find and update values of srcval,tempval,tarval..
{
int m,n=val+1,n1=0;
for(m=0;m<=val-1;m++)
{
if(a[m] < n && a[m]!=0)
{
n = a[m];
n1++;
}
}
n = n1<1?0:n;
srcval = n;
n=val+1;
n1=0;
for(m=0;m<=val-1;m++)
{
if(b[m] < n && b[m]!=0)
{
n1++;
n = b[m];
}
}
n = n1<1?0:n;
tempval = n;
n=val+1;
n1=0;
for(m=0;m<=val-1;m++ )
{
if(c[m] < n&& c[m]!=0)
{
n = c[m];
n1++;
}
}
n = n1<1?0:n;
tarval = n;
}
void move(int grt[], int sml[])//move from target to src
{
int i=0,j=0;
int m,n=val+1,n1=0;
for(m=0;m<=val-1;m++)
{
if(grt[m] < n && grt[m]!=0)
{
n = grt[m];
i=m;
n1++;
}
}
n = n1<1?0:n;
grt[i]=0;
for(m=0;m<=val-1;m++)
{
if(sml[m]==0)
{
sml[m]=n;
break;
}
}
find();
}
Could any one please help with the iterative soln of hanoi tower.
without using printf("Move disk %d from tower%d to tower%d\n",i,d=(d+(i&1?dir:1-dir))%3+1,d) like abstract algorithm. i have manually iterated array. is there any better way out.