/*Tri-color Hanoi*/#include<stdio.h>voidHanoiintDisksCharSourceCharTempChartarget) { if(Disks = =1) {printf ("move disk from%c to%c \ n", source, target); printf ("move disk from%c to%c \ n", source, target); printf ("move disk from%c to%c \ n", source, target); } Else{Hanoi (disks-1, source, target, temp); Hanoi (1, source, temp, target); Hanoi (Disks-1, temp, source, target); }}voidHanoi3colors (intdisks) { CharSource ='A'; Chartemp ='B'; Chartarget ='C'; inti; if(Disks = =3) {printf ("move disk from%c to%c \ n", source, temp); printf ("move disk from%c to%c \ n", source, temp); printf ("move disk from%c to%c \ n", source, target); printf ("move disk from%c to%c \ n", temp, target); printf ("move disk from%c to%c \ n", temp, source); printf ("move disk from%c to%c \ n", Target, temp); } Else{Hanoi (disks/3-1, source, temp, target); printf ("move disk from%c to%c \ n", source, temp); printf ("move disk from%c to%c \ n", source, temp); printf ("move disk from%c to%c \ n", source, temp); Hanoi (Disks/3-1, Target, temp, source); printf ("move disk from%c to%c \ n", temp, target); printf ("move disk from%c to%c \ n", temp, target); printf ("move disk from%c to%c \ n", temp, target); Hanoi (Disks/3-1, source, target, temp); printf ("move disk from%c to%c \ n", Target, temp); printf ("move disk from%c to%c \ n", Target, temp); Hanoi (Disks/3-1, temp, source, target); printf ("move disk from%c to%c \ n", source, temp); for(i = disks/3-1; i >0; i--) { if(I >1) {Hanoi (I-1, target, source, temp); } printf ("move disk from%c to%c \ n", target, source); printf ("move disk from%c to%c \ n", target, source); if(I >1) {Hanoi (I-1, temp, source, target); } printf ("move disk from%c to%c \ n", source, temp); } }}intMain () {intN; printf ("Please enter the number of disks:"); scanf ("%d", &N); Hanoi3colors (n); return 0;}
"Tri-color Hanoi"