/*
* @Author: HerrDing (1103037805@qq.com)
* @Date: 2022-04-20 22:20:13
* @LastEditors: HerrDing
* @LastEditTime: 2022-04-21 22:35:46
* @FilePath: /刷题/hanoi.cpp
* @Description: solve Hanoi problem
*
* Copyright (c) 2022 by Ding, All Rights Reserved.
*/
#include <iostream>
#include <string>
static int timer = 1;
using namespace std;
class Hanoi
{
public:
void Process(int i, string from, string end, string other)
{
/** base case*/
if (i < 1)
{
return;
}
if (i == 1)
{
cout << "the " << timer << "th move pateform " << i << " from " << from << " to " << end << endl;
timer++;
return;
}
/** 1. move plateform named [1,i-1] from \form to \other*/
Process(i - 1, from, other, end);
/** 2. move plateform named i to end*/
cout << "the " << timer << "th move pateform " << i << " from " << from << " to " << end << endl;
timer++;
/** 3. move plateform named [1,i-1] from \other to \end*/
Process(i - 1, other, end, from);
return;
}
};
int main(int argc, char **argv)
{
Hanoi test;
test.Process(2, "A", "C", "B");
return 0;
}
汉诺塔问题C++实现
最新推荐文章于 2024-06-17 08:19:26 发布