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策略全…...
Ubuntu系统下交叉编译openssl
一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机:Ubuntu 20.04.6 LTSHost:ARM32位交叉编译器:arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...
深入浅出:JavaScript 中的 `window.crypto.getRandomValues()` 方法
深入浅出:JavaScript 中的 window.crypto.getRandomValues() 方法 在现代 Web 开发中,随机数的生成看似简单,却隐藏着许多玄机。无论是生成密码、加密密钥,还是创建安全令牌,随机数的质量直接关系到系统的安全性。Jav…...
unix/linux,sudo,其发展历程详细时间线、由来、历史背景
sudo 的诞生和演化,本身就是一部 Unix/Linux 系统管理哲学变迁的微缩史。来,让我们拨开时间的迷雾,一同探寻 sudo 那波澜壮阔(也颇为实用主义)的发展历程。 历史背景:su的时代与困境 ( 20 世纪 70 年代 - 80 年代初) 在 sudo 出现之前,Unix 系统管理员和需要特权操作的…...
智能仓储的未来:自动化、AI与数据分析如何重塑物流中心
当仓库学会“思考”,物流的终极形态正在诞生 想象这样的场景: 凌晨3点,某物流中心灯火通明却空无一人。AGV机器人集群根据实时订单动态规划路径;AI视觉系统在0.1秒内扫描包裹信息;数字孪生平台正模拟次日峰值流量压力…...
css3笔记 (1) 自用
outline: none 用于移除元素获得焦点时默认的轮廓线 broder:0 用于移除边框 font-size:0 用于设置字体不显示 list-style: none 消除<li> 标签默认样式 margin: xx auto 版心居中 width:100% 通栏 vertical-align 作用于行内元素 / 表格单元格ÿ…...
图表类系列各种样式PPT模版分享
图标图表系列PPT模版,柱状图PPT模版,线状图PPT模版,折线图PPT模版,饼状图PPT模版,雷达图PPT模版,树状图PPT模版 图表类系列各种样式PPT模版分享:图表系列PPT模板https://pan.quark.cn/s/20d40aa…...
视频行为标注工具BehaviLabel(源码+使用介绍+Windows.Exe版本)
前言: 最近在做行为检测相关的模型,用的是时空图卷积网络(STGCN),但原有kinetic-400数据集数据质量较低,需要进行细粒度的标注,同时粗略搜了下已有开源工具基本都集中于图像分割这块,…...
NXP S32K146 T-Box 携手 SD NAND(贴片式TF卡):驱动汽车智能革新的黄金组合
在汽车智能化的汹涌浪潮中,车辆不再仅仅是传统的交通工具,而是逐步演变为高度智能的移动终端。这一转变的核心支撑,来自于车内关键技术的深度融合与协同创新。车载远程信息处理盒(T-Box)方案:NXP S32K146 与…...
Netty从入门到进阶(二)
二、Netty入门 1. 概述 1.1 Netty是什么 Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. Netty是一个异步的、基于事件驱动的网络应用框架,用于…...
CppCon 2015 学习:Reactive Stream Processing in Industrial IoT using DDS and Rx
“Reactive Stream Processing in Industrial IoT using DDS and Rx” 是指在工业物联网(IIoT)场景中,结合 DDS(Data Distribution Service) 和 Rx(Reactive Extensions) 技术,实现 …...
