扩展的河内塔
Extended Hanoi Tower
问题定义
我正在尝试编写一个C 程序来解决Extended Hanoi Tower
问题。
扩展的河内塔与标准的河内问题相似。不同之处在于,奇数环在 a (1、3、5,...)中,甚至戒指都在 b (2,2,4,6,..)。问题问,我们如何在递归 Way中将所有戒指转移到 c ?
我已经写了我到目前为止所做的。任何帮助我实施自己的方法或实施该计划的任何想法的任何帮助都将不胜感激!谢谢!
规则
1.一次只能移动一个磁盘。
2.不得将磁盘放在较小的磁盘上。
3.输出应包含必要的动作,而不是必要的最小动作
4.磁盘的总数可能为偶数或奇数。
5.实施应具有递归。
方法1
我们假设该问题已针对当前在A和B中的N-1环解决了。这意味着它们都被移至C和C的2n-2订购环。之后,我们必须处理a&B.这意味着我们必须将较小的戒指放在较大的环上(从B到A)。之后,我们必须将环与C(2n-2)中的环与A(2)中的环合并。最后,我们必须使用标准的河内解决方案来实现我们的目标,并将所有戒指移至c。
实施1
int main()
{
long int n;
cin >> n;
// move odd & even rings to the first shaft, recursively
customHanoi(n);
// move all rings from first shaft to the destination shaft, recursively
standardHanoi(n);
return 0;
}
// the shaft holds the odd rings: A
// the shaft holds the even rings: B
// the final destination shaft: C
void customHanoi(int n, char A, char B, char C)
{
if (n == 1)
return;
else if (n == 2)
{
cout << B << " " << A << endl;
return;
}
else if (n == 3)
{
cout << A << " " << C << endl;
cout << B << " " << A << endl;
cout << C << " " << A << endl;
}
else
{
// here we have some missing code !
}
}
// a recursive function to find the solution for standard hanoi
void standardHanoi(int n, char from, char helper, char to)
{
// the base condition
if (n == 1)
{
cout << from << " " << to << endl;
return;
}
// the recursive calls
standardHanoi(n - 1, from, to, helper);
cout << from << " " << to << endl;
standardHanoi(n - 1, helper, from, to);
}
realeated Resources
链接
我们可以从b。
我们可以在标准河内的A上将B中的1,2放在B上超过3。所有其他磁盘都更大,可以忽略。
我们可以通过标准河内从B上放置1,2,3。所有其他磁盘都更大,可以忽略。........
当我们将所有磁盘安排在一个极点上时,它们要么在A(如果磁盘总数为奇数)或B(偶数)。
通过标准河内将它们全部移至C。
,尽管这可能被认为是一种迭代方法,而您要求递归的方法。
因此,递归:假设总共有n
磁盘。通过递归应用将n-1
磁盘移动到C。将n-1
磁盘从C移至A(n
是奇数)或B(n
偶数)通过标准河内。通过标准河内将所得的n
磁盘移动到C。
这与您建议的非常相似,除了n
代表磁盘总数(环)。它也不是纯粹的递归。
非常感谢 @all-ness!
这是可能的解决方案之一。
希望这可以帮助 !:)
#include <iostream>
using namespace std;
// function declarations
void customHanoi(long int, char = 'A', char = 'B', char = 'C');
void standardHanoi(long int, char = 'A', char = 'B', char = 'C');
int main()
{
// initialize the variable
long int n;
// getting the number of rings
cin >> n;
// move odd & even rings to the first shaft, recursively
// after that move all rings from first shaft to the destination shaft
customHanoi(n);
// our program finished successfully
return 0;
}
// A: the shaft that holds odd rings
// B: the shaft that holds even rigns
// C: the final destination shaft
void customHanoi(long int n, char A, char B, char C)
{
// initialize the variable
static long int level = 1;
// we can't handle zero/negative disk
if (n < 1)
return;
// the base condition of recursion
if (level == n)
{
// now, we moved all rings to the first shaft
// so, we have to move them to the destination shaft
standardHanoi(n);
// finish the execution of recursion
return;
}
// reordering the disks
// based on even or odd number of disks & current level
if (n % 2 == 1)
{
if (level % 2 == 1)
standardHanoi(level, A, C, B);
else
standardHanoi(level, B, C, A);
}
else
{
if (level % 2 == 1)
standardHanoi(level, B, C, A);
else
standardHanoi(level, A, C, B);
}
// go to the next level, it helps us control the flow
level++;
// the recursive calls
customHanoi(n);
}
// a recursive function to find the solution for standard hanoi
void standardHanoi(long int n, char from, char helper, char to)
{
// the base condition
if (n == 1)
{
cout << from << " " << to << endl;
return;
}
// the recursive calls
standardHanoi(n - 1, from, to, helper);
cout << from << " " << to << endl;
standardHanoi(n - 1, helper, from, to);
}
- 为什么在全局范围内使用"extern int a"似乎不行?
- 内置函数可查看CPP中的成员变量
- 内联扩展编译为函数调用 [C++]
- c++ 变量在宏的扩展中没有在这个范围内声明
- 在 *.cpp 文件中实现的 c++ 函数/方法永远不会内联扩展吗?
- 在编译的哪个阶段,c++ 中的内联函数实际上会扩展
- 扩展的河内塔
- 包含开关的功能在C 中不扩展内联
- 关闭内联扩展,但让优化存在
- 为什么在未赋值的操作数内进行包扩展会导致最后一个元素
- 外壳扩展DLL-如果用户在文件夹空白区域内单击,如何捕获文件夹路径
- 内联扩展可能不起作用的情况
- 如何在c++中使用Detours扩展程序内函数而不进入无限循环
- LabWindows中内置的Tcl的C扩展在升级到Tcl 8.6后崩溃
- while 语句中的错误 - 包含 while 的函数未内联扩展
- 扩展内联汇编:输入和输出相同的变量
- 内联命名空间和扩展命名空间
- 在调用者内部扩展被调用者的指导原则是什么(内联-编译器优化)
- 扩展非类型形参包来定义带有非类型形参的内部类模板是否合法?
- 扩展SWIG内置类