I. INTRODUCTION
Hanoi Tower Problem is a typical example of recursion, and the explanation in the book is very detailed, it is helpful to understand the working mechanism of C language function and function transfer, it is worth seeing. Moreover, recursion in my opinion and division, DP, greedy and so on is a very beautiful thought, it is worth learning!!!
Two. cpp file
1 //3_3.cpp2 /**3 Author:zhaoyu4 Email:[email protected]5 date:2016-6-86 note:realize My textbook << data structure (C-language version) >>7 */8 //Page9#include <cstdio>Ten intCNT =0; One voidMoveCharAintNCharb) A { -printf"%i\tmove disk%d from%c to%c\n", ++CNT, N, a, b); - } the voidHanoiintNCharXCharYCharz) - { - if(1==N) - { +Move (x,1, z); - } + Else A { atHanoi (n1, X, z, y); - Move (x, n, z); -Hanoi (n1, y, x, z); - } - } - intMainintargcChar Const*argv[]) in { - intN; toscanf"%d", &n); +Hanoi (N,'X','Y','Z'); - return 0; the}
3_3.cpp
Three. Testing
Three:
Five:
Easy to see, moving N, need 2^n-1 times.
Data structure algorithm C language implementation (10)---3.3 Stack and recursive implementation