用C++解决汉诺塔问题

/*
时间:2018.1.25
作者:小岛的水
*/

#include<iostream>

using namespace std;

//调用递归函数解决汉诺塔问题

int i = 1;                 
//全局变量来记录布数

int main()

{

 void move(int n, char from, char to);

 void hanoi(int n, char from, char zhongjian, char to);

 int n;

 cout << “请输入盘子个数:” << endl;

 cin >> n;

 char a = ‘A’, b = ‘B’, c = ‘C’;

 hanoi(n, a, b, c);

 return 0;

} void move(int n,char from,char to)

{

 cout << “第” << i++ << “步:将第” << n << “号盘子” << from << “–>” << to << endl;

    //每调用一次move函数,步数加一

} void hanoi(int n, char from, char zhongjian, char to)

{

 void move(int n, char from, char to);

 if (n == 1)

  move(1, from,to);

 else

 {

  hanoi(n – 1,from,zhongjian,to);

  move(n, from, to);

  hanoi(n – 1, zhongjian, from,to);

 }

}

    原文作者: 汉诺塔问题
    原文地址: https://blog.csdn.net/lsh_water/article/details/79163529
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞