1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 5 static int step = 0; 6 void move ( char sour, char dest ) 7 { 8 printf ( "move from %c to %c \n", sour, dest ); 9 } 10 11 void hanoi ( int n, char sour, char temp, char dest ) 12 { 13 if ( n == 1 ) 14 { 15 move ( sour, dest ); 16 ++step; 17 } 18 else 19 { 20 hanoi ( n-1, sour, dest, temp ); 21 move ( sour,dest ); 22 ++step; 23 hanoi ( n-1, temp, sour, dest ); 24 } 25 } 26 int main ( int argc, char **argv ) 27 { 28 int n = 4; 29 hanoi ( n, ‘A‘, ‘B‘, ‘C‘ ); 30 printf ( "Total steps is %d\n", step ); 31 return 0; 32 }
转载于:http://www.cnblogs.com/DanielZheng/archive/2011/08/20/2146453.html
时间: 2024-10-15 10:49:37