扩展的河内塔

Extended Hanoi Tower

本文关键字:内塔 扩展      更新时间:2023-10-16

问题定义
我正在尝试编写一个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);
}