汉诺塔问题新思路(更简单的描述)

只需要完成两步就可以解决任意阶汉诺塔问题,且满足最小移动步数。

假设有1,2,3三个柱子。依次对1,2,3三个柱子进行循环,正在循环的当前柱子设为X,设圆盘为N个,如果N为奇数则将2,3柱子对调,如果N为偶数不变:
初始
第一步:如果满足条件,最上方的圆盘可以移动到相邻的下一个柱子X+1上,则设当前柱为X,并将最上方的圆盘移动到相邻的下一个柱子X+1上。
第一步
第二步:接着再将X柱最上方的圆盘移动到下下个柱子X+2柱子上,如果X最上方圆盘不能移动到X+2,就将X+2最上方圆盘移动到X。
第二步
1柱子操作结束,开始循环2柱子:
第一步
第二步
2柱子操作结束,开始循环3柱子:
第一步
第二步
3柱子操作结束,开始循环1柱子:
第一步
所有流程结束。
以下为C++代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

struct hanoiCol
{
   int pos = -1;
   char colTag;
   int *data;
};

void move(int n, char from, char to)
{
   cout << "from " << from << " move " << n << " to " << to << endl;
}

void hanoi()
{
   int blocks = 0, target = 2;
   printf("输入汉诺塔层数: ");
   scanf("%d", &blocks);
   struct hanoiCol cols[3];
   char colName[3] = {'A', 'B', 'C'};
   
   for(int i=0; i<3; i++)
   {
       cols[i].colTag = colName[i];
       cols[i].data = (int *)malloc(blocks * sizeof(int));
       
       if(i == 0)
       {
           for(int j=0, k=blocks-1; j<=blocks-1; j++, k--)
           {
               cols[i].data[j] = k + 1;
               ++cols[i].pos;
           }
       }
   }
   if(blocks % 2 != 0)
   {
       struct hanoiCol temp = cols[1];
       cols[1] = cols[2];
       cols[2] = temp;
       target = 1;
   }
   
   int plus = 0;
   while(cols[target].pos != (blocks-1))
   {
       for(int j=0; j<3 && cols[target].pos != (blocks-1); j++)
       {
           int mainidx = j;
           int step_next = mainidx + 1;
           
           for(int i=0; i<2 && cols[target].pos != (blocks-1); i++)
           {
               step_next = (step_next >= 3 ? step_next-3 : step_next);
               
               if(cols[mainidx].pos != -1 && (cols[step_next].pos == -1 || cols[mainidx].data[cols[mainidx].pos] < cols[step_next].data[cols[step_next].pos]))
               {
                   move(cols[mainidx].data[cols[mainidx].pos], cols[mainidx].colTag, cols[step_next].colTag);
                   cols[step_next].data[++(cols[step_next].pos)] = cols[mainidx].data[(cols[mainidx].pos)--];
               }
               else if(cols[step_next].pos != -1)
               {
                   move(cols[step_next].data[cols[step_next].pos], cols[step_next].colTag, cols[mainidx].colTag);
                   cols[mainidx].data[++(cols[mainidx].pos)] = cols[step_next].data[(cols[step_next].pos)--];
               }
               step_next++;
               printf("\n");
           }
       }
   }
}

int main()
{
   hanoi();
   printf("Hello World\n");
   return 0;
}

欢迎指正,纠错。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值