汉诺塔问题的递归算法是递归问题中最最经典的问题。它将递归技术优势表现的淋漓尽致。即无需模拟每一个步骤的具体情况,只需按算法流程设置出口和递归。
剩下的交由计算机去做即可,极大的降低了问题的复杂度。
算法代码
/** 将上面的n个圆盘通过help从current移动到target **/ void hanoi(int n,char current,char target,char help){ if(n>0){ hanoi(n-1,current,help,target); cout<<"move "<<n<<" from "<<current<<" to "<<target<<endl; hanoi(n-1,help,target,current); } }
欲将上面的n个圆盘从a移动到b,则需要
先将上面的n-1个圆盘通过b从a移动到c;
再将第n个圆盘从a移动到b;
最后再将上面的n-1个圆盘从c通过a移动到b
原文:https://www.cnblogs.com/fangexuxiehuihuang/p/14163651.html