none
Evaluate this... RRS feed

  • Question

  • //This is my CPP program for the TOWERS OF HANOI problem... Try it in TC and suggest for improvements...


     


    #include<stdio.h>
    #include<math.h>
    #include<graphics.h>
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>

     

    int list[3][15],start=1;
    int r,r1=5,nD,nn,timer,code1=1;
    int offset=50,step_number=0;

     

    void spindle(int n,int list[])
    {
     setcolor(BLACK);
     setlinestyle(SOLID_LINE,1,3);

     int n1=n;

     if(n!=0)
      r=(102/n);
     else
      n=nn;
     int w=(n*r);
     setcolor(WHITE);
     moveto(offset-10,300);
     lineto(offset+w+10,300);
     moveto(offset+w/2,300);

     int h=(n*r)+50;

     lineto(offset+w/2,300-h);

     int mf=n-8;

     if(n<=11)
      mf=20;
     int i,x;
     if(nD>11)
      x=r+n-mf-13;
     else
      x=r+n-mf-7;

     int bottom=299,right,left,top,y=300-r1-5;
     int color=1;

     for(i=0;i<n1;i++)
     {
      setfillstyle(SOLID_FILL , color);
      setcolor(color);
      right=offset+w-x,left=offset+x,top=y;
      if(list[i]!=0)
      {
       bar(left,top,right,bottom);
       bottom=top-1;
       y-=r1+5;
      }
      x+=15-n;
      color++;
      if(color==7)
       color++;
     }
     offset+=210;
    }


    void animate(int n)
    {
     clrscr();
     if(code1==1)
      cout<<"To exit press 'e',  else press any key to continue\n ";
     cout<<"Step number : "<<step_number++;
     offset=50;
     spindle(n,list[0]);
     spindle(n,list[1]);
     spindle(n,list[2]);
     if(start==1)
     {
      cout<<"\n\nPress any key to start ";
      getch();
      start++;
     }

     char ch;

     for(timer=0;timer<15000;timer++)
      printf(" \b");
     if(code1==1)
      ch=getch();
     if(ch=='e'||ch=='E')
      exit(0);
    }

     

    void solve(int n,int source,int dest,int temp)
    {
     if(n==2)
     {
      list[source-1][nD-n+1]=0;
      list[temp-1][nD-n+1]=1;
      animate(nD);
      list[source-1][nD-n]=0;
      list[dest-1][nD-n]=1;
      animate(nD);
      list[temp-1][nD-n+1]=0;
      list[dest-1][nD-n+1]=1;
      animate(nD);
     }
     else
     {
      solve((n-1),source,temp,dest);
      list[source-1][nD-n]=0;
      list[dest-1][nD-n]=1;
      animate(nD);
      solve((n-1),temp,dest,source);
     }
     return;
    }


    void main()
    {
     int n,i,j;
    start:
     int g_driver,g_mode;
     step_number=0;
     r1=5;
     start=1;
     code1=1;
     offset=50;
     g_driver=DETECT;
     g_mode=EGAHI;
     setgraphmode(EGAHI);
     initgraph(&g_driver,&g_mode,"C:\\tc\\bin\\");
     setbkcolor(BLUE);
     clrscr();
     cout<<"Enter the number of disks : ";
     cin>>n;
     nD=nn=n;
     for(i=0;i<15;i++)
      list[0][i]=list[1][i]=list[2][i]=0;
     for(i=0,j=n;i<n;i++,j--)
      list[0][i]=n;
     if(n<2||n>14)
     {
      cout<<"Enter a number in the range 2 to 14";
      fflush(stdin);
      getch();
      return;
     }
     clrscr();
     spindle(n,list[0]);
     spindle(n,list[1]);
     spindle(n,list[2]);
     offset=50;
        codestart:
     clrscr();
     cout<<"\nTotal number of steps = "<<pow(2,n)-1;
     cout<<"\n1.To view as animation Press 'a'\n2.To view as single move steps Press 'n'\nEnter a code(a,n): ";
     char codec;
     cin>>codec;
     if(codec!='a'&&codec!='A'&&codec!='n'&&codec!='N')
     {
      goto codestart;
     }
     if(codec=='n' || codec =='N')
      code1=1;
     else
      code1=2;
     animate(nD);
     solve(n,1,3,2);
     printf("\n\t\t\t\tEND OF MOVES");
     gotoxy(20,20);
     printf("Do you want to continue ?:");
     codec=getch();
     if(codec=='Y'||codec=='y')
      goto start;
    }


    Thursday, May 31, 2007 4:48 AM

Answers

  • you seem to love programming.
    that's nice buddy.
    would you love to program virtual router with me?
    Thursday, May 31, 2007 2:25 PM
  • I would be glad to work with you.. Presently am at hols, so lotta time in hand.. Well, we can discuss abt the virtual router in IM.. My gtalk id is raghuramdcbe@gmail.com...

    Let me know your gtalk id or yahoo IM id...

    Thursday, May 31, 2007 2:59 PM

All replies

  • you seem to love programming.
    that's nice buddy.
    would you love to program virtual router with me?
    Thursday, May 31, 2007 2:25 PM
  • I would be glad to work with you.. Presently am at hols, so lotta time in hand.. Well, we can discuss abt the virtual router in IM.. My gtalk id is raghuramdcbe@gmail.com...

    Let me know your gtalk id or yahoo IM id...

    Thursday, May 31, 2007 2:59 PM