汉诺塔的思考

众所周知的汉若塔问题是典型的递归问题。

基本思想是:如果要完成N个盘子的汉诺塔问题,首先思考如何完成N-1和盘子的汉若塔问题,为了便于思考,我们选择三个简单的汉诺塔问题进行设计。
to代表目标柱子,from代表原始柱子,depend代表借助的柱子


代码如下:
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;


void function3(int n, char from, char depend, char to)
{
 if (n == 1)
 {
  cout << "将盘子" << n << "从" << from << "塔----->" << to<< "塔" << endl;

 }
 else
 {
  function3(n - 1, from, to, depend);      //完成n-1借助于to柱子从初始柱子到中间借助柱子
  cout << "将盘子" << n << "从" << from << "塔----->" << to << "塔"<<endl;
  function3(n - 1, depend, from, to);      //完成n-1借助于from柱子从之间借助柱子到目标柱子
 }
  
}
int main()
{
 int n = 5;
 function3(n, 'a', 'b', 'c');
}


原文地址:https://www.cnblogs.com/liumantang/p/11830401.html