SGU 202 The Towers of Hanoi Revisited (DFS+预处理)

汉诺塔 THE TOWER OF HANOI THE TOWER OF HANOI 问题描述 汉诺塔(也称河内塔)是有法国数学家爱德华·卢卡斯于1883年发明的一道智力题。它源于印度的一个古老传说:大梵天创造世界的时候做了三根钻石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令一组牧师把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。据说牧师们夜以继日地工作,当他们完成任务时,那座塔就将坍塌,世界也将毁灭。 汉诺塔问题是这样规定的:如下图,一个由8个圆盘组成的塔, 阅读详情

题目描述:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2338


The Towers of Hanoi Revisited

Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge

You all must know the puzzle named ��The Towers of Hanoi��. The puzzle has three pegs and N discsof different radii, initially all disks are located on the first peg, ordered by their radii - the largest atthe bottom, the smallest at the top. In a turn you may take the topmost disc from any peg and move itto another peg, the only rule says that you may not place the disc atop any smaller disk. The problemis to move all disks to the last peg making the smallest possible number of moves.

There is the legend that somewhere in Tibet there is a monastery where monks tirelessly move disksfrom peg to peg solving the puzzle for 64 discs. The legend says that when they finish, the end of theworld would come. Since it is well known that to solve the puzzle you need to make 2N - 1 moves, asmall calculation shows that the world seems to be a quite safe place for a while.

However, recent archeologists discoveries have shown that the things can be a bit worse. Themanuscript found in Tibet mountains says that the puzzle the monks are solving has not 3 but Mpegs. This is the problem, because when increasing the number of pegs, the number of moves needed tomove all discs from the first peg to the last one following the rules described, decreases dramatically.Calculate how many moves one needs to move N discs from the first peg to the last one when thepuzzle has M pegs and provide the scenario for moving the discs.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

Input file contains N and M (1 <= N <= 64, 4 <= M <= 65).


Output

On the first line output L - the number of moves needed to solve the puzzle. Next L lines mustcontain the moves themselves. For each move print the line of the form

move <disc-radius> from <source-peg> to <target-peg>

if the disc is moved to the empty peg or

move <disc-radius> from <source-peg> to <target-peg> atop <target-top-disc-radius>

if the disc is moved atop some other disc.

Disc radii are integer numbers from 1 to N, pegs are numbered from 1 to M.


Sample Input

1

5 4


Sample Output

13
move 1 from 1 to 3
move 2 from 1 to 2
move 1 from 3 to 2 atop 2
move 3 from 1 to 4
move 4 from 1 to 3
move 3 from 4 to 3 atop 4
move 5 from 1 to 4
move 3 from 3 to 1
move 4 from 3 to 4 atop 5
move 3 from 1 to 4 atop 4
move 1 from 2 to 1
move 2 from 2 to 4 atop 3
move 1 from 1 to 4 atop 2


题目大意:

  给定N(1<= N <=64)个盘子和M(4<= M <= 65)根柱子,问把N个盘子从1号柱子移动到M号柱子所需要的最少步数,并且输出移动过程。


解题思路:

  对于整个移动的过程可以概括为如下:

先把k个盘子,通过j跟柱子移动到中间的某根柱子上,步数:f[k][j];

把剩下的i-k个盘子通过剩下的j-1跟柱子移到第j跟柱上,步数:f[i-k][j-1];

最后再把中间的k个盘子移到J柱上,步数:f[k][j]。

  为防止重复计算,且该题状态有限(64*65),因此作为预处理将所有的状态所需最小步数求出。
再通过DFS按照上述思路打印出移动步骤即可。


复杂度分析:

时间复杂度 O(n*n)
空间复杂度 O(n*n)


#include <cstdio>
#include <stack>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 70;
const unsigned long long INF=0xffffffffffffffff;///定义无限大
unsigned long long f[maxn][maxn]; ///f[i][j]表示i个盘,通过j个柱子,全程移动所需要的最少次数。
int pre[maxn][maxn]; ///pre[i][j]表示f[i][j]的最优方案是先将最小的path[i][j]个盘移动到中间某根的柱子上
int n,m;///n个盘子 m个柱子
stack<int>stk[maxn];
bool used[maxn];
void init()
{
   memset(f,INF,sizeof(f));
   for(int i=3;i<=65;i++)
   {
       f[0][i]=0;
       f[1][i]=1;
   }
   for(int i=1;i<=64;i++)
   {
       f[i][3]=2*f[i-1][3]+1;
       pre[i][3]=i-1;
   }
   for(int i=2;i<=64;i++)
    for(int j=4;j<=65;j++)
     for(int k=1;k<i;k++)
     {
       if(f[i][j]>2*f[k][j]+f[i-k][j-1])
       {
           f[i][j]=2*f[k][j]+f[i-k][j-1];
           pre[i][j]=k;
       }
    }
}
void dfs(int nt,int mt,int scr,int des)
{
   if(nt==1)
   {
       if(stk[des].size())
         printf("move %d from %d to %d atop %d\n",stk[scr].top(),scr,des,stk[des].top());
       else
         printf("move %d from %d to %d\n",stk[scr].top(),scr,des);
       stk[des].push(stk[scr].top());
       stk[scr].pop();
       return;
   }
   int peg=0;
   for(int i=1;i<=m;i++)
   {
       if(scr!=i && des!=i && !used[i])
       {
           peg=i;
           break;
       }
   }
   dfs(pre[nt][mt],mt,scr,peg);
   used[peg]=true;
   dfs(nt-pre[nt][mt],mt-1,scr,des);
   used[peg]=false;
   dfs(pre[nt][mt],mt,peg,des);
}
int main()
{
   init();
   int N;
   cin>>N;
   while(N--)
   {
       cin>>n>>m;
       for(int i=1;i<=m;i++) while(!stk[i].empty()) stk[i].pop();
       for(int i=n;i>=1;i--) stk[1].push(i);
       memset(used,false,sizeof(used));
       printf("%llu\n",f[n][m]);
       dfs(n,m,1,m);
   }
   return 0;
}



The Towers of Hanoi recursion 汉诺塔 C++ What is Towers of Hanoi ? Figure1 shows an tower of hanoi setup with three discs on the first pole. Note that all the discs are a different size. The a The Rules The goal is move all dis 阅读详情

相关推荐

YOLO26算法心电图十二导联目标检测+训练好的模型+368张数据集+pyqt可视化界面.zip

附训练好的 YOLO 权重与 PyQt 可视化检测界面,页面底部含可视化效果预览,毕设/课设开箱即用。 【数据集概况】 · 检测类别(中文):[导联I(I), 导联II(II), 导联III(III), 胸导联V1(V1), 胸导联V2(V2), 胸导联V3(V3), 胸导联V4(V4), 胸导联V5(V5), 胸导联V6(V6), 加压肢体导联aVF(aVF), 加压肢体导联aVL(aVL), 加压肢体导联aVR(aVR)] · 训练集:313 张 · 验证集:37 张 · 测试集:18 张 · 总计:368 张 该数据集聚焦于临床心电图信号的精准识别与定位,其核心检测目标为标准12导联心电图中的各个独立导联波形区域,涵盖肢体导联(I、II、III)与加压肢体导联(aVR、aVL、aVF),以及胸前导联(V1至V6),适用于心电图自动分析系统中对各导联位置的快速准确标注与分类。数据采集场景真实还原了医院心电图室或急诊监护环境下的标准心电图记录纸,波形清晰、基线稳定,具有高度临床参考价值。... 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 57 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.9708** mAP50-95 | 0.6344 Precision | 0.8990 Recall | 0.9191 train/box_loss | 1.2231 train/cls_loss | 0.9703 val/box_loss | 1.2779 val/cls_loss | 0.... 使用场景:毕业设计、课程设计、深度学习入门、工业场景落地验证。

The Towers of Hanoi Revisited---(多柱汉诺塔)

Description You all must know the puzzle named "The Towers of Hanoi". The puzzle has three pegs and N discs of different radii, initially all disks are located on the first peg, ordered b...

weixin_30788731的博客 212

用友YonSuite产品实操手把手教学(图文版)

用友YonSuite

ct341688的博客 8156

sgu-202 The Towers of Hanoi Revisited

题目大意: hanoi问题 输入给你两个数n,m,表示有n个盘子,m个柱子,问最少需要多少步使得n个盘子从柱子1->m,并且输出方案 输出方法move i from s to t 表式移动盘子i从s放到t上,如果t上原本有盘子j,那么输出move i from s to t atop j 解题思路:     首先我们用dp求出最短的步数 dp思路摘自http:/

zzj的专栏 883

闲人闲谈PS之一项目库存跨公司业务STO解决方案--SAP闲人的开篇

写这篇文章之前,简单说一下发这篇文章的动机。也算是在SAP这一行干了快10多年,在对一个模块了解越来越深入,越发觉自己的SAP知识浅薄。总的来说,和多数顾问一样,对于**标准功能**的应用业务场景理解还是存在或多或少的知识盲区。 因为一个偶然,看到SAP团子君的文章,分享了很多干货文章。这也是自己一直想做的时候,不管水平高低与否,分享本身就意味着提升。好了废话不多说,先在优快云上发一篇吧,困惑自己将近10年的问题的解决方案。 业务场景:在项目型企业中,通常会有这样的业务,母公司和客...

SAP闲人的博客 9267

利用Openlayers4实现地图遮罩效果(二)

之前实现的遮罩效果,在深色背景的底图上,对要突出的区域采用半透明遮罩,以此来突出该区域。但是有时底图颜色偏白,这时候不再适合对要突出的区域采用遮罩,而是要对突出区域之外进行遮罩处理。两种效果的对比入下图: ...

此爱如少年 6398

THE TOWER OF HANOI

关于hanoi的问题,我就不重复叙述了,在看Cmath的时候看到一道例题,能够测试对递归概念的理解递归问题的练习2在hanoi问题的基础上,对盘的移动规则做了进一步限制,每个盘不能在A、B之间直接移动,必须从其中一柱开始,经过mid,再移动到另一柱上。如图 将所有盘移动到B所需步数为H(n)将盘2、。。。、n 作为一个整体(取名为n-1个盘),移动到B或从B移动回A的步数则

Little Mermaid's Obstinacy 366

【STM32Cube HAL】输入捕获(六)——PWM测量

实验内容:使用通用定时器输入捕获测量信号的脉宽(检测按键按下的时间)。 一、原理图 二、 CubeMX配置 Step1.打开 STM32CubeMX,点击“New Project”,选择芯片型号,STM32F103VETx。 Step2.选择时钟源,并配置时钟树。选择Crystal/Ceramic Resonator,并配置系统时钟为72M。 Step3.配置SYS,我们这里选择的是Serial Wire。(正常情况配置不配置不影响,debug可以使用。但是你不可以把这...

HAHAHA 1万+

Acdream 1219 The Towers of Hanoi Revisited(递归汉诺塔问题)

传送门The Towers of Hanoi Revisited Special Judge Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description You all must know t

代码改变世界 3534

手机刷双系统

刷机教程(https://www.geekeyes.cn/forum.php/forum.php?mod=viewthread&amp;tid=492) 解锁教程(http://www.gioov.com/index.php/1386)   解锁教程 cmd下 进入adb目录( 如果环境变量配置了adb 可以直接在cmd 下输入命令) 手机连接电脑,确保usb 调试打开,然后手机连接电脑,...

超宇的博客 1万+

YOLOv8/v9 遥感图像目标检测实战指南:建筑物检测与变化监测

遥感图像在城市规划、灾害评估、农业监管等领域应用广泛,但传统目标检测方法存在效率低、精度不足、难以适配遥感图像大尺寸、多尺度目标等问题。本文以“建筑物检测与变化监测”为核心,基于DOTA、NWPU VHR-10等公开数据集,系统讲解YOLOv8/v9在遥感图像中的实战流程。从数据获取、格式转换、模型训练优化,到大图切片推理、建筑物变化监测,全程提供可复现的代码与参数配置。通过针对性优化(旋转增强、切片推理等),模型在NWPU VHR-10数据集上mAP50达0.85以上,可适配卫星影像、无人机航拍等多场景

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 519

汉诺塔问题(Towers of Hanoi)

汉诺塔问题的思维方式很重要,正向和逆向都很容易绕晕,要从中间开始展开

黑白的记忆 3320

微信小程序- 云开发上传并部署云函数时报错ResourceNotFound.Function, 未找到函数版本,请创建后再试。(小程序上传部署云函数提示错误,开发控制台中的云函数没有删除,详细排查)

微信小程序——云函数上传失败,微信小程序——解决上传并部署云函数时报错ResourceNotFound.Function, 未找到函数版本,请创建后再试。微信小程序上传云函数失败,微信小程序云开发|Error: ResourceNotFound.FunctionName, FunctionName 指定的资源不存在。微信云开发报错(Error: ResourceNotFound.FunctionName,微信小程序云函数报错,上传云函数 sum 失败 Error:,上传部署我们的云函数 2. 报错 错误信息

🏆 前端领域优质创作者 4313

sgu 202

Dp

Dash 459

RK3568 Android14 调试 RTL8211F 千兆以太网 (RGMII)

调试 RTL8211F 的核心在于:确认地址(看电阻贴片情况)和 搞定延时(配置 rgmii-id 或微调 delay 值)。一旦这两点对上了,RK3568 的千兆网络就能稳定运行。希望这篇结合原理图的实战记录能帮到你!

qq_33427070的博客 1648

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

ZOJ-2338 The Towers of Hanoi Revisited 输出汉诺塔的最优解移动过程

题意:给定N(1&lt;= N &lt;=64)个盘子和M(4&lt;= M &lt;= 65)根柱子,问把N个盘子从1号柱子移动到M号柱子所需要的最少步数,并且输出移动过程。 分析:设f[i][j]表示将i个盘通过j个柱子从第一根柱子移动到最后一根柱子所需要的最少次数,f[i][j]的得到的最优移动方案是先将最小的path[i][j]个盘移动到某一根柱子上,然后通过一个dfs输出移动次序。 ...

weixin_34313182的博客 190

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

SGU 202 The Towers of Hanoi Revisited (DP+递归)

转载请注明出处,谢谢http://blog.youkuaiyun.com/ACM_cxlove?viewmode=contents    by---cxlove 题意 :n个圆盘,m个柱子的汉诺塔输出步骤。 http://acm.sgu.ru/problem.php?contest=0&problem=202 经典的递归问题,见具体数学第一章 先DP求出最短步骤,并记录路径。 然后 递归输出

窝不是爱酱,喵~~~~ 2086

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

汉诺塔(the Tower of Hanoi )

  问题       初始时,给定3根柱子A,B,C,N个大小不一的圆盘,这些圆盘从小到大排列在其中的一根柱子上,假设为A,现在要通过B,将这N个圆盘全部移动到C柱子上,每次只能移动一个,移动的过程中不许出现大盘子放在小盘上面的情况,问完成这个任务至少需要将盘子移动多少次? 分析和程序       设N个盘子至少需要T(N)次移动,显然T(0)=0,T(1)=1。考虑到要将N个盘子从A通过...

gigglesun的专栏 2819

Towers of Hanoi 递归和构建 java 走地牙 CTCI 8.6

Towers of Hanoi:In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order ...

yangzhuhappy的博客 367

POJ 3572 Hanoi Towers 可能会

woniupengpeng的专栏 316

hanoi塔问题解析(一) c++实现

什么是hanoi塔? 汉诺塔问题:古代有一个梵塔,塔内有三个座A、B、C,A座上有64个盘子,盘子大小不等,大的在下,小的在上。有一个和尚想把这64个盘子从A座移到B座,但每次只能允许移动一个盘子,并且在移动过程中,3个座上的盘子始终保持大盘在下,小盘在上。如下图 问题解答 问题定义 我们把左边的柱子叫做A,中间的柱子叫做B,右边的柱子叫做C hanoi塔的搬运过程; i

woshinannan的专栏 2万+

ZOJ Problem Set - 2338 The Towers of Hanoi Revisited DFS+预处理

ZOJ Problem Set - 2338 The Towers of Hanoi Revisited题目描述:  题目链接:Problem Set - 2338 The Towers of Hanoi Revisited题目大意:  给定N(1<= N <=64)个盘子和M(4<= M <= 65)根柱子,问把N个盘子从1号柱子移动到M号柱子所需要的最少步数,并且输出移动过程。解题思路:  对于

Running Boy的AC Dream 734

uva254 - Towers of Hanoi 递归

Towers of Hanoi  In 1883, Edouard Lucas invented, or perhaps reinvented, one of the most popular puzzles of all times - the Tower of Hanoi, as he called it - which is still used today in

988

Towers of Hanoi

Towers of Hanoi 汉诺塔问题与汉诺塔四塔问题 标签(空格分隔): ACM 算法 最常见的汉诺塔问题: 汉诺塔问题是一个经典的问题。汉诺塔(Hanoi Tower),又称河内塔,源于印度一个古老传说。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,任何时候,在小圆盘...

qq_17175221的博客 707
上一篇: POJ 1163 The Triangle(三种搜索方式)
下一篇: ZOJ 3469 Food Delivery
hellohelloC
博客等级 码龄12年 15粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值