home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / byteunix.lzh / byte.1 / hanoi.c < prev    next >
C/C++ Source or Header  |  1990-05-11  |  1KB  |  54 lines

  1. /*******************************************************************************
  2.  *  The BYTE UNIX Benchmarks - Release 2
  3.  *          Module: hanoi.c   SID: 2.4 4/17/90 16:45:34
  4.  *          
  5.  *******************************************************************************
  6.  * Bug reports, patches, comments, suggestions should be sent to:
  7.  *
  8.  *    Ben Smith or Rick Grehan at BYTE Magazine
  9.  *    bensmith@bixpb.UUCP    rick_g@bixpb.UUCP
  10.  *
  11.  *******************************************************************************
  12.  *  Modification Log:
  13.  *  $Header: hanoi.c,v 3.5 87/08/06 08:11:14 kenj Exp $
  14.  *
  15.  ******************************************************************************/
  16.  
  17. char SCCSid[] = "@(#) @(#)hanoi.c:2.4 -- 4/17/90 16:45:34";
  18. #define PRINT 0
  19. #define DISK 3
  20. #define other(i,j) (6-(i+j))
  21. int num[4];
  22. long cnt;
  23. main(argc,argv)
  24. char **argv;
  25. {
  26.     int disk;
  27.     disk  = DISK;
  28.     if(argc > 1)disk = atoi(argv[1]);
  29.     num[1] = disk;
  30.     if(PRINT)printf("Start %d on A\n",disk);
  31.     mov(disk,1,3);
  32.     printf("For %d disks, %ld moves\n",disk,cnt);
  33.  
  34.     exit(0);
  35. }
  36.  
  37. mov(n,f,t)
  38. {
  39.     int o;
  40.     if(n == 1) {
  41.         num[f]--;
  42.         num[t]++;
  43.         if(PRINT)printf("Move from %d to %d, result: A:%d B:%d C%d\n",
  44.             f,t,num[1],num[2],num[3]);
  45.         cnt++;
  46.         return;
  47.     }
  48.     o = other(f,t);
  49.     mov(n-1,f,o);
  50.     mov(1,f,t);
  51.     mov(n-1,o,t);
  52.     return;
  53. }
  54.