汉若塔问题(递归)

 1 #include 
 2 #include 
 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 }
View Code

 

 

转载于:http://www.cnblogs.com/DanielZheng/archive/2011/08/20/2146453.html

转载于:https://www.cnblogs.com/WDKER/p/5187341.html

你可能感兴趣的:(汉若塔问题(递归))