Hanoi ( 2022 ICPC Southeastern Europe Regional Contest )
Hanoi ( 2022 ICPC Southeastern Europe Regional Contest )
The original problem “Towers of Hanoi” is about moving n n n circular disks of distinct sizes between 3 3 3 rods. In one move, the player can move only the top disk from one rod to the top of another. The disks should always be placed one over the other in decreasing order of sizes. Initially, the n n n disks reside on rod 1 1 1 and the goal is to have them all n n n reside on rod 3 3 3.
There is a myth of an Indian temple where priests are solving the instance of “Towers of Hanoi” with 64 64 64 disks since the beginning of time. It is believed that once this instance is solved, the world will end.
However, this “Relaxed Hanoi” problem is not that apocalyptic. In fact, it is pretty optimistic as once you correctly solve it, you will get a positive verdict. In this variation, rod 1 1 1 is not subject to the order rule. In other words, the disks on rod 1 1 1 can reside in any order at any point of time.
For an instance of the “Relaxed Hanoi” problem, provide at most 2 ⋅ n 2 2 \cdot n^2 2⋅n2 moves that solve the problem.
Input
The first line of the input contains a single integer n n n ( 1 ≤ n ≤ 500 1 \leq n \leq 500 1≤n≤500) — the number of disks.
The second line of the input contains n n n integers p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,…,pn — the sizes of the disks on rod 1 1 1 from bottom to top (the first is the bottom disk and the n n n-th is the top disk).
It is guaranteed that p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,…,pn form a permutation (in other words, each number from 1 1 1 to n n n appears exactly once amongst p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,…,pn).
Output
The first line of output should contain a single integer k k k ( 0 ≤ k ≤ 2 ⋅ n 2 0 \leq k \leq 2 \cdot n^2 0≤k≤2⋅n2) — the number of moves.
Each of the following k k k lines should contain two integers a i a_i ai and b i b_i bi ( 1 ≤ a i , b i ≤ 3 1 \leq a_i, b_i \leq 3 1≤ai,bi≤3), describing a move of the topmost disk from rod a i a_i ai to rod b i b_i bi.
In the end, all n n n disks should reside on rod number 3 3 3.
Example
Input
5
2 1 5 3 4
Output
9
1 2
1 2
1 3
2 1
2 3
1 3
1 2
1 3
2 3
Note
The provided output has only 9 9 9 moves. For n = 5 n=5 n=5, any solution with at most 50 50 50 moves will suffice.
- After moves $1, $ 2 and 3 3 3, the configuration is ⟨ 2 , 1 ⟩ ⟨ 4 , 3 ⟩ ⟨ 5 ⟩ \langle 2, 1 \rangle \langle 4, 3 \rangle \langle 5 \rangle ⟨2,1⟩⟨4,3⟩⟨5⟩.
- Move 4 4 4 is legal as the first rod is relaxed. The configuration is ⟨ 2 , 1 , 3 ⟩ ⟨ 4 ⟩ ⟨ 5 ⟩ \langle 2, 1, 3 \rangle \langle 4 \rangle \langle 5 \rangle ⟨2,1,3⟩⟨4⟩⟨5⟩.
- After moves 5 5 5 and 6 6 6, the configuration is ⟨ 2 , 1 ⟩ ⟨ ⟩ ⟨ 5 , 4 , 3 ⟩ \langle 2, 1 \rangle \langle \rangle \langle 5, 4, 3 \rangle ⟨2,1⟩⟨⟩⟨5,4,3⟩.
- Last moves 7 7 7, 8 8 8 and 9 9 9 determine the final configuration is ⟨ ⟩ ⟨ ⟩ ⟨ 5 , 4 , 3 , 2 , 1 ⟩ \langle \rangle \langle \rangle \langle 5, 4, 3, 2, 1 \rangle ⟨⟩⟨⟩⟨5,4,3,2,1⟩.
题面描述
问题背景: 经典的汉诺塔问题涉及将 n n n 个大小不同的圆盘在 3 3 3 根柱子之间移动。每次只能移动最上面的一个圆盘,并且任何时候都必须保持圆盘按大小递减的顺序叠放。初始时,所有圆盘都在柱子 1 1 1 上,目标是将它们全部移动到柱子 3 3 3 上。
问题变种: 在这个“Relaxed Hanoi”问题中,柱子 1 1 1 不受顺序规则的限制。也就是说,柱子 1 1 1 上的圆盘可以以任意顺序叠放。
任务: 对于给定的“Relaxed Hanoi”问题实例,提供一个最多包含 2 ⋅ n 2 2 \cdot n^2 2⋅n2 步的移动序列,将所有圆盘从柱子 1 1 1 移动到柱子 3 3 3。
输入:
- 第一行包含一个整数 n n n ( 1 ≤ n ≤ 500 1 \leq n \leq 500 1≤n≤500),表示圆盘的数量。
- 第二行包含 n n n 个整数 p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,…,pn,表示柱子 1 1 1 上从下到上的圆盘大小。保证这些整数是 1 1 1 到 n n n 的一个排列。
输出:
- 第一行输出一个整数 k k k ( 0 ≤ k ≤ 2 ⋅ n 2 0 \leq k \leq 2 \cdot n^2 0≤k≤2⋅n2),表示移动的步数。
- 接下来的 k k k 行每行包含两个整数 a i a_i ai 和 b i b_i bi,表示将柱子 a i a_i ai 最上面的圆盘移动到柱子 b i b_i bi。
示例:
输入:
5
2 1 5 3 4
输出:
9
1 2
1 2
1 3
2 1
2 3
1 3
1 2
1 3
2 3
题解
问题分析:
- 由于柱子 1 1 1 不受顺序规则的限制,我们可以利用这一点来简化移动过程。
- 目标是将所有圆盘从柱子 1 1 1 移动到柱子 3 3 3,并且最终柱子 3 3 3 上的圆盘必须按大小递减的顺序叠放。
解题思路:
- 初始化: 将所有圆盘从柱子 1 1 1 移动到柱子 3 3 3,但保持柱子 3 3 3 上的圆盘按大小递减的顺序。
- 移动策略:
- 使用一个辅助柱子(柱子 2 2 2)来临时存放圆盘。
- 当柱子 1 1 1 上的圆盘大小小于当前柱子 3 3 3 上的最小圆盘时,直接将其移动到柱子 3 3 3。
- 否则,将柱子 3 3 3 上的较小圆盘移动到柱子 1 1 1,腾出空间,然后再将较大的圆盘移动到柱子 3 3 3。
- 复杂度: 由于每个圆盘最多需要 2 n 2n 2n 步移动,总步数不会超过 2 n 2 2n^2 2n2。
代码分析
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define close ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
set<int> st[3];void solved()
{int n;cin >> n;vector<int> arr(n + 1, 0);for (int i = 1; i <= n; i++){cin >> arr[n - i + 1]; // 反转输入顺序,方便处理}vector<array<int, 2>> ans;int start = 3;set<int> st2;for (int i = 1; i <= n; i++){if (st2.empty()){st2.insert(arr[i]);ans.push_back({1, start}); // 直接将圆盘移动到柱子3}else{int v = *(st2.begin());if (v >= arr[i]){st2.insert(arr[i]);ans.push_back({1, start}); // 直接将圆盘移动到柱子3}else{ans.push_back({1, 2}); // 将圆盘移动到柱子2vector<int> path;while (!st2.empty() && *(st2.begin()) < arr[i]){path.push_back(*(st2.begin()));ans.push_back({start, 1}); // 将柱子3上的较小圆盘移动到柱子1st2.erase(st2.begin());}st2.insert(arr[i]);ans.push_back({2, 3}); // 将圆盘移动到柱子3for (int j = 0; j < path.size(); j++){st2.insert(path[j]);ans.push_back({1, 3}); // 将柱子1上的圆盘移动到柱子3}}}}cout << ans.size() << endl;for (auto [i, j] : ans){cout << i << " " << j << endl;}
}signed main()
{close;solved();
}
代码分析:
- 输入处理: 读取圆盘数量 n n n 和圆盘大小数组 a r r arr arr,并将数组反转以便从顶部开始处理。
- 移动策略:
- 使用
st2
集合来维护柱子 3 3 3 上的圆盘大小。 - 对于每个圆盘,如果它小于或等于柱子 3 3 3 上的最小圆盘,则直接移动到柱子 3 3 3。
- 否则,将柱子 3 3 3 上的较小圆盘移动到柱子 1 1 1,腾出空间后再将当前圆盘移动到柱子 3 3 3。
- 使用
- 输出: 输出移动步数和每一步的移动操作。
复杂度分析:
- 每个圆盘最多需要 2 n 2n 2n 步移动,因此总步数不会超过 2 n 2 2n^2 2n2,满足题目要求。
总结
该问题通过放松柱子 1 1 1 的顺序规则,简化了汉诺塔问题的移动过程。通过合理的移动策略,可以在 2 n 2 2n^2 2n2 步内将所有圆盘移动到目标柱子。代码实现中使用了集合来维护柱子 3 3 3 上的圆盘大小,并通过临时移动较小圆盘来腾出空间,确保最终柱子 3 3 3 上的圆盘按大小递减的顺序叠放。
相关文章:
Hanoi ( 2022 ICPC Southeastern Europe Regional Contest )
Hanoi ( 2022 ICPC Southeastern Europe Regional Contest ) The original problem “Towers of Hanoi” is about moving n n n circular disks of distinct sizes between 3 3 3 rods. In one move, the player can move only the top disk from on…...

Matplotlib基础01( 基本绘图函数/多图布局/图形嵌套/绘图属性)
Matplotlib基础 Matplotlib是一个用于绘制静态、动态和交互式图表的Python库,广泛应用于数据可视化领域。它是Python中最常用的绘图库之一,提供了多种功能,可以生成高质量的图表。 Matplotlib是数据分析、机器学习等领域数据可视化的重要工…...
SMU寒假训练第二周周报
训练情况 本周是第二周,训练情况比第一周好一点点,也仅仅是好一点点,经过春节以及后遗症,牛客更是打的稀烂,还不如去年,都不知道自己在干嘛,训练赛情况也非常糟糕,还要去搞社会实践…...

解锁全新视界:一键畅享 360 度全景图与多格式转换
软件介绍 各位朋友,大家好!今天要给大家引荐一款超实用的全景图转换“神器”——Pano2VR Pro 的最新版本。在当今这个追求极致视觉体验的时代,它宛如一把神奇的钥匙,能够解锁全新的视觉领域,将平平无奇的不同角度图像…...
python:面向对象案例烤鸡翅
自助烤鸡翅的需求: 1.烤鸡翅的时间和对应的状态: 0-4min :生的 4-7min:半生不熟 7-12min:熟了 12min以上:烤糊了 2.添加调料: 客户根据自己的需求添加 定义烤鸡翅的类、属性和方法,显示对象的信息 …...
游戏外挂原理解析:逆向分析与DLL注入实战(植物大战僵尸
目录 1.前言2.外挂类型3.前置知识4.CE查找基质4.1 逐步分析4.2 暴力搜索5.实现数值外挂6.dll导入表注入7.实现行为外挂(无敌类型)8.源码下载与外挂进阶本篇原文为:游戏外挂原理解析:逆向分析与DLL注入实战(植物大战僵尸)。 更多C++进阶、rust、python、逆向等等教程,可…...
【10.10】队列-设计自助结算系统
一、题目 请设计一个自助结账系统,该系统需要通过一个队列来模拟顾客通过购物车的结算过程,需要实现的功能有: get_max():获取结算商品中的最高价格,如果队列为空,则返回 -1add(value):将价格为…...
android的ViewModel和LiveData 简介
ViewModel ViewModel 的优势 ViewModel 的替代方案是保存要在界面中显示的数据的普通类。在 activity 或 Navigation 目的地之间导航时,这可能会造成问题。此时,如果您不利用保存实例状态机制存储相应数据,系统便会销毁相应数据。ViewModel…...

Linux系统之free命令的基本使用
Linux系统之free命令的基本使用 一、free命令介绍二、free命令的使用帮助2.1 free命令的帮助信息2.2 free命令帮助解释 三、free命令的基本使用3.1 显示内存使用情况3.2 新增总计条目3.3 显示内存详细信息 四、注意事项 一、free命令介绍 free 命令是 Linux 系统中用于显示系统…...
大模型赋能网络安全整体应用流程概述
一、四个阶段概述 安全大模型的应用大致可以分为四个阶段: 阶段一主要基于开源基础模型训练安全垂直领域的模型; 阶段二主要基于阶段一训练出来的安全大模型开展推理优化、蒸馏等工序,从而打造出不同安全场景的专家模型,比如数据安全领域、安全运营领域、调用邮件识别领…...

SpringCloud - Nacos注册/配置中心
前言 该博客为Nacos学习笔记,主要目的是为了帮助后期快速复习使用 学习视频:7小快速通关SpringCloud 辅助文档:SpringCloud快速通关 一、简介 Nacos官网:https://nacos.io/docs/next/quickstart/quick-start/ Nacos /nɑ:kəʊ…...
面试准备——Java理论高级【笔试,面试的核心重点】
集合框架 Java集合框架是面试中的重中之重,尤其是对List、Set、Map的实现类及其底层原理的考察。 1. List ArrayList: 底层是动态数组,支持随机访问(通过索引),时间复杂度为O(1)。插入和删除元素时&#…...

AI伴读-清华大学104页《DeepSeek:从入门到精通》
辅助工具:deepseek、豆包AI伴读 官网:DeepSeekDeepSeek, unravel the mystery of AGI with curiosity. Answer the essential question with long-termism.https://www.deepseek.com/https://www.deepseek.com/清华大学104页《DeepSeek:从入…...

unity学习34:角色相关3,触发器trigger,铰链 hingejoint 等 spring joint, fixed joint
目录 1 触发的实现条件 1.1 碰撞的的实现条件 1.2 触发的实现条件 1.3 触发器trigger,直接拿 碰撞器collider修改下配置即可 2 触发器相关实验:触发开门效果 2.0 目标 2.1 player物体的属性 2.2 新建一个trigger 物体 2.3 新建一个被trigger 控…...

HarmonyOS Next 方舟字节码文件格式介绍
在开发中,可读的编程语言要编译成二进制的字节码格式才能被机器识别。在HarmonyOS Next开发中,arkts会编译成方舟字节码。方舟字节码长什么样呢?我们以一个demo编译出的abc文件: 二进制就是长这样,怎么去理解呢&…...

计算机视觉语义分割——Attention U-Net(Learning Where to Look for the Pancreas)
计算机视觉语义分割——Attention U-Net(Learning Where to Look for the Pancreas) 文章目录 计算机视觉语义分割——Attention U-Net(Learning Where to Look for the Pancreas)摘要Abstract一、Attention U-Net1. 基本思想2. Attention Gate模块3. 软注意力与硬注意力4. 实验…...

html 列动态布局
样式说明: /* 列动态布局,列之间以空格填充 */ li {display: flex;/* flex-direction: column; */justify-content: space-between; }...

DeepSeek开源多模态大模型Janus-Pro部署
DeepSeek多模态大模型部署 请自行根据电脑配置选择合适环境配置安装conda以及gitJanus 项目以及依赖安装运行cpu运行gpu运行 进入ui界面 请自行根据电脑配置选择合适 本人家用电脑为1060,因此部署的7B模型。配置高的可以考虑更大参数的模型。 环境配置 安装conda…...

DeepSeek结合Langchain的基本用法
DeepSeek结合Langchain的基本用法 DeepSeek 基于Openai接口规范的Prompt应答Deepseek结合LangchainDeepSeek 基于langchain的结构化返回 DeepSeek 基于Openai接口规范的Prompt应答 首先我们需要先基于pip 安装 pip install openai最开始我们先熟悉如何使用openai的接口规范&a…...

Redis持久化的两种方式:RDB和AOF
redis中的数据存储在缓存中,如果没有持久化的策略,Redis一旦宕机,那么将会导致数据丢失;因此redis提供了以下两种持久化方式:RDB和AOF 一般来说,大部分公司对这两种方式都是同时开启的 一、RDB RDB策略全…...

C++初阶-list的底层
目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

JavaScript 中的 ES|QL:利用 Apache Arrow 工具
作者:来自 Elastic Jeffrey Rengifo 学习如何将 ES|QL 与 JavaScript 的 Apache Arrow 客户端工具一起使用。 想获得 Elastic 认证吗?了解下一期 Elasticsearch Engineer 培训的时间吧! Elasticsearch 拥有众多新功能,助你为自己…...
【位运算】消失的两个数字(hard)
消失的两个数字(hard) 题⽬描述:解法(位运算):Java 算法代码:更简便代码 题⽬链接:⾯试题 17.19. 消失的两个数字 题⽬描述: 给定⼀个数组,包含从 1 到 N 所有…...
django filter 统计数量 按属性去重
在Django中,如果你想要根据某个属性对查询集进行去重并统计数量,你可以使用values()方法配合annotate()方法来实现。这里有两种常见的方法来完成这个需求: 方法1:使用annotate()和Count 假设你有一个模型Item,并且你想…...
大语言模型如何处理长文本?常用文本分割技术详解
为什么需要文本分割? 引言:为什么需要文本分割?一、基础文本分割方法1. 按段落分割(Paragraph Splitting)2. 按句子分割(Sentence Splitting)二、高级文本分割策略3. 重叠分割(Sliding Window)4. 递归分割(Recursive Splitting)三、生产级工具推荐5. 使用LangChain的…...

转转集团旗下首家二手多品类循环仓店“超级转转”开业
6月9日,国内领先的循环经济企业转转集团旗下首家二手多品类循环仓店“超级转转”正式开业。 转转集团创始人兼CEO黄炜、转转循环时尚发起人朱珠、转转集团COO兼红布林CEO胡伟琨、王府井集团副总裁祝捷等出席了开业剪彩仪式。 据「TMT星球」了解,“超级…...
鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院查看报告小程序
一、开发环境准备 工具安装: 下载安装DevEco Studio 4.0(支持HarmonyOS 5)配置HarmonyOS SDK 5.0确保Node.js版本≥14 项目初始化: ohpm init harmony/hospital-report-app 二、核心功能模块实现 1. 报告列表…...
汇编常见指令
汇编常见指令 一、数据传送指令 指令功能示例说明MOV数据传送MOV EAX, 10将立即数 10 送入 EAXMOV [EBX], EAX将 EAX 值存入 EBX 指向的内存LEA加载有效地址LEA EAX, [EBX4]将 EBX4 的地址存入 EAX(不访问内存)XCHG交换数据XCHG EAX, EBX交换 EAX 和 EB…...
CRMEB 中 PHP 短信扩展开发:涵盖一号通、阿里云、腾讯云、创蓝
目前已有一号通短信、阿里云短信、腾讯云短信扩展 扩展入口文件 文件目录 crmeb\services\sms\Sms.php 默认驱动类型为:一号通 namespace crmeb\services\sms;use crmeb\basic\BaseManager; use crmeb\services\AccessTokenServeService; use crmeb\services\sms\…...

FFmpeg:Windows系统小白安装及其使用
一、安装 1.访问官网 Download FFmpeg 2.点击版本目录 3.选择版本点击安装 注意这里选择的是【release buids】,注意左上角标题 例如我安装在目录 F:\FFmpeg 4.解压 5.添加环境变量 把你解压后的bin目录(即exe所在文件夹)加入系统变量…...