《汉诺塔问题的详解.ppt》由会员分享,可在线阅读,更多相关《汉诺塔问题的详解.ppt(50页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、/汉诺塔汉诺塔 ABCABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔 ABC/汉诺塔汉诺塔/汉诺塔汉诺塔#include void hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn”,a,c);hanoi(n-1,b,a,c);void main()int n;printf(Input the number of diskes:n
2、“);scanf(“%d”,&n);hanoi(n,A,B,C);Stackn a b cn a b cOutput/汉诺塔汉诺塔 void main()int n;printf(Input the number of diskes:n“);scanf(“%d”,&n);hanoi(n,A,B,C);Stackn a b cn a b cH(3,A,B,C)3 A B COutputH(n,A,B,C)/汉诺塔汉诺塔 void main()int n;printf(Input the number of diskes:n“);scanf(“%d”,&n);hanoi(n,A,B,C);Stac
3、kn a b cn a b cH(3,A,B,C)3 A B COutputH(n,A,B,C)void hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);/汉诺塔汉诺塔Stackn a b cn a b cH(3,A,B,C)3 A B C2 A C B1 A B COutputH(n,A,B,C)H(n-1,a,c,b)H(n-1,a,c,b)H(0,A,C,B)H(n-1,a,c,b)0 A C BH(2,A,C,B)H(1,A,B,C)void
4、hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BH(1,A,B,C)1 A B COutputH(n,A,B,C)H(n-1,a,c,b)H(n-1,a,c,b)H(0,A,C,B)H(n-1,a,c,b)0 A C Bvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);pr
5、intf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BH(1,A,B,C)1 A B COutputA CH(n,A,B,C)H(n-1,a,c,b)H(n-1,a,c,b)coutvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)
6、2 A C BH(1,A,B,C)1 A B COutputA CH(n,A,B,C)H(n-1,a,c,b)H(n-1,a,c,b)H(0,B,A,C)H(n-1,b,a,c)0 B A Cvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BH(1,A,B,C)1 A B COutputA CH(n,A,B,C)H(n-1,a,c,b)H(
7、n-1,a,c,b)H(0,B,A,C)H(n-1,b,a,c)0 B A Cvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BH(1,A,B,C)1 A B COutputA CH(n,A,B,C)H(n-1,a,c,b)H(n-1,a,c,b)void hanoi(int n,char a,char b,char c)if (n=1)h
8、anoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BOutputA CA BH(n,A,B,C)H(n-1,a,c,b)coutvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BO
9、utputA CA BH(n,A,B,C)H(n-1,a,c,b)1 C A BH(n-1,b,a,c)H(1,C,A,B)void hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BOutputA CA BH(n,A,B,C)H(n-1,a,c,b)H(1,C,A,B)1 C A BH(n-1,b,a,c)H(0,C,B,A)H(n-1,a,c,b
10、)0 C B Avoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BOutputA CA BH(n,A,B,C)H(n-1,a,c,b)H(1,C,A,B)1 C A BH(n-1,b,a,c)H(0,C,B,A)H(n-1,a,c,b)0 C B Avoid hanoi(int n,char a,char b,char c)if (n=1)
11、hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BOutputA CA BH(n,A,B,C)H(n-1,a,c,b)H(1,C,A,B)1 C A BH(n-1,b,a,c)C Bcoutvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(
12、3,A,B,C)3 A B CH(2,A,C,B)2 A C BOutputA CA BH(n,A,B,C)H(n-1,a,c,b)H(1,C,A,B)1 C A BH(n-1,b,a,c)C BH(0,A,C,B)H(n-1,b,a,c)0 A C Bvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BOutputA CA BH(n,A,B
13、,C)H(n-1,a,c,b)H(1,C,A,B)1 C A BH(n-1,b,a,c)C BH(0,A,C,B)H(n-1,b,a,c)0 A C Bvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BOutputA CA BH(n,A,B,C)H(n-1,a,c,b)H(1,C,A,B)1 C A BH(n-1,b,a,c)C Bvoid
14、 hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BOutputA CA BH(n,A,B,C)H(n-1,a,c,b)C Bvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b c
15、n a b cH(3,A,B,C)3 A B CH(2,A,C,B)2 A C BOutputA CA BH(n,A,B,C)H(n-1,a,c,b)C BA Ccoutvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(n-1,b,a,c)A Cvoid hanoi(int n,char
16、 a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A C1 B C AH(n-1,b,a,c)H(n-1,a,c,b)A CH(0,B,A,C)H(n-1,a,c,b)0 B A CH(1,B,C,A)void hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf
17、(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(1,B,C,A)1 B C AH(n-1,b,a,c)H(n-1,a,c,b)A CH(0,B,A,C)H(n-1,a,c,b)0 B A Cvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a
18、b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(1,B,C,A)1 B C AH(n-1,b,a,c)H(n-1,a,c,b)A CB Acoutvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(1,B,C,
19、A)1 B C AH(n-1,b,a,c)H(n-1,a,c,b)A CB AH(0,C,B,A)H(n-1,b,a,c)0 C B Avoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(1,B,C,A)1 B C AH(n-1,b,a,c)H(n-1,a,c,b)A CB AH(0,C,
20、B,A)H(n-1,b,a,c)0 C B Avoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(1,B,C,A)1 B C AH(n-1,b,a,c)H(n-1,a,c,b)A CB Avoid hanoi(int n,char a,char b,char c)if (n=1)hanoi
21、(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(1,B,C,A)1 B C AH(n-1,b,a,c)H(n-1,a,c,b)A CB AB Ccoutvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b
22、 cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(n-1,b,a,c)A CB A1 A B CH(n-1,b,a,c)B CH(1,A,B,C)void hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(n-1,b,a,
23、c)A CB AH(1,A,B,C)1 A B CH(n-1,b,a,c)B CH(0,A,C,B)H(n-1,a,c,b)0 A C Bvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(n-1,b,a,c)A CB AH(1,A,B,C)1 A B CH(n-1,b,a,c)B CH(
24、0,A,C,B)H(n-1,a,c,b)0 A C Bvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(n-1,b,a,c)A CB AH(1,A,B,C)1 A B CH(n-1,b,a,c)B CA Ccoutvoid hanoi(int n,char a,char b,char c
25、)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);/汉诺塔汉诺塔void hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);couta c=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(n-1,b,a,c)A CB A1 A B
26、 CB CA CH(1,A,B,C)H(n-1,b,a,c)void hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(n,A,B,C)C BH(2,B,A,C)2 B A CH(n-1,b,a,c)A CB AB CA Cvoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);prin
27、tf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);Stackn a b cn a b cH(3,A,B,C)3 A B COutputA CA BH(3,A,B,C)C BA CB AB CA COvervoid hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);OverStackn a b cn a b cOutputA CA BC BA CB AB CA C/汉诺塔汉诺塔void hanoi(int n,char a,char b,char c)if (n=1)hanoi(n-1,a,c,b);printf(“%c-%cn“,a,c);hanoi(n-1,b,a,c);