码迷,mamicode.com
首页 > 编程语言 > 详细

[算法]汉诺塔

时间:2019-10-22 15:36:17      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:std   using   string   cpp   space   lse   class   math   hello   

#include <iostream>
#include <cmath>

using namespace std;
int hannuota(int n, string a, string b, string c)
{
    if (n == 1)
    {
        //只有一个盘子的情况下直接将第一个塔上的盘子移动到第三个塔
        printf("塔%s------>塔%s\n", a.c_str(), c.c_str());
    }
    else {
        //1.先将第一个塔的n-1个盘子全部通过第三个塔移动到第二个塔上
        hannuota(n - 1, a, c, b);
        //2.再将剩下的一个盘子移动到第三个塔上
        printf("塔%s------>塔%s\n", a.c_str(), c.c_str());
        //3.最后将第二个塔上的盘子通过第一个塔移动到第三个塔上
        hannuota(n - 1, b, a, c);
    }
    return 1;
}
int main()
{
    
    printf("盘子移动如下:\n");
    hannuota(3, "A", "B", "C");//三个盘子,三个塔


    cout << "hello world" << endl;
    return 0;
}

技术图片

[算法]汉诺塔

标签:std   using   string   cpp   space   lse   class   math   hello   

原文地址:https://www.cnblogs.com/tailiang/p/11719524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!