home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
pc
/
source
/
byteunix.lzh
/
byte.1
/
hanoi.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-05-11
|
1KB
|
54 lines
/*******************************************************************************
* The BYTE UNIX Benchmarks - Release 2
* Module: hanoi.c SID: 2.4 4/17/90 16:45:34
*
*******************************************************************************
* Bug reports, patches, comments, suggestions should be sent to:
*
* Ben Smith or Rick Grehan at BYTE Magazine
* bensmith@bixpb.UUCP rick_g@bixpb.UUCP
*
*******************************************************************************
* Modification Log:
* $Header: hanoi.c,v 3.5 87/08/06 08:11:14 kenj Exp $
*
******************************************************************************/
char SCCSid[] = "@(#) @(#)hanoi.c:2.4 -- 4/17/90 16:45:34";
#define PRINT 0
#define DISK 3
#define other(i,j) (6-(i+j))
int num[4];
long cnt;
main(argc,argv)
char **argv;
{
int disk;
disk = DISK;
if(argc > 1)disk = atoi(argv[1]);
num[1] = disk;
if(PRINT)printf("Start %d on A\n",disk);
mov(disk,1,3);
printf("For %d disks, %ld moves\n",disk,cnt);
exit(0);
}
mov(n,f,t)
{
int o;
if(n == 1) {
num[f]--;
num[t]++;
if(PRINT)printf("Move from %d to %d, result: A:%d B:%d C%d\n",
f,t,num[1],num[2],num[3]);
cnt++;
return;
}
o = other(f,t);
mov(n-1,f,o);
mov(1,f,t);
mov(n-1,o,t);
return;
}