Tower of Hanoi
Example of The Solution of Tower of Hanoi Problem
Program below simulate the solver of the legendary Tower of Hanoi.
/* title: towerOfHanoi author: aeriqusyairi date: Jan28 2012 */ #include<stdio.h> #include<math.h> void hanoi( int, char, char, char ); int main(){ int disk, moves; printf("Enter the number of disk you want to play with:"); scanf("%d", &disk); moves = pow( 2, disk ) - 1; printf("\nThe number of moves required is = %d\n", moves ); hanoi(disk, 'A', 'C', 'B' ); system("pause"); return 0; } void hanoi(int x, char from, char to, char temp){ if( x == 1 ) printf("Move disk from %c to %c\n", from, to ); else{ hanoi(x - 1, from, temp, to ); printf("Move disk from %c to %c\n", from, to ); hanoi( x - 1, temp, to, from ); } }
No comments:
Post a Comment