当前位置: 首页 > news >正文

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 2n2 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 1n500) — 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 0k2n2) — 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 1ai,bi3), 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,14,35.
  • 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,345.
  • 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,15,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 2n2 步的移动序列,将所有圆盘从柱子 1 1 1 移动到柱子 3 3 3

输入:

  • 第一行包含一个整数 n n n ( 1 ≤ n ≤ 500 1 \leq n \leq 500 1n500),表示圆盘的数量。
  • 第二行包含 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 0k2n2),表示移动的步数。
  • 接下来的 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 1 移动到柱子 3 3 3,但保持柱子 3 3 3 上的圆盘按大小递减的顺序。
  2. 移动策略:
    • 使用一个辅助柱子(柱子 2 2 2)来临时存放圆盘。
    • 当柱子 1 1 1 上的圆盘大小小于当前柱子 3 3 3 上的最小圆盘时,直接将其移动到柱子 3 3 3
    • 否则,将柱子 3 3 3 上的较小圆盘移动到柱子 1 1 1,腾出空间,然后再将较大的圆盘移动到柱子 3 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 &#xff08; 2022 ICPC Southeastern Europe Regional Contest &#xff09; 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库&#xff0c;广泛应用于数据可视化领域。它是Python中最常用的绘图库之一&#xff0c;提供了多种功能&#xff0c;可以生成高质量的图表。 Matplotlib是数据分析、机器学习等领域数据可视化的重要工…...

SMU寒假训练第二周周报

训练情况 本周是第二周&#xff0c;训练情况比第一周好一点点&#xff0c;也仅仅是好一点点&#xff0c;经过春节以及后遗症&#xff0c;牛客更是打的稀烂&#xff0c;还不如去年&#xff0c;都不知道自己在干嘛&#xff0c;训练赛情况也非常糟糕&#xff0c;还要去搞社会实践…...

解锁全新视界:一键畅享 360 度全景图与多格式转换

软件介绍 各位朋友&#xff0c;大家好&#xff01;今天要给大家引荐一款超实用的全景图转换“神器”——Pano2VR Pro 的最新版本。在当今这个追求极致视觉体验的时代&#xff0c;它宛如一把神奇的钥匙&#xff0c;能够解锁全新的视觉领域&#xff0c;将平平无奇的不同角度图像…...

python:面向对象案例烤鸡翅

自助烤鸡翅的需求&#xff1a; 1.烤鸡翅的时间和对应的状态&#xff1a; 0-4min :生的 4-7min:半生不熟 7-12min&#xff1a;熟了 12min以上&#xff1a;烤糊了 2.添加调料&#xff1a; 客户根据自己的需求添加 定义烤鸡翅的类、属性和方法&#xff0c;显示对象的信息 …...

游戏外挂原理解析:逆向分析与DLL注入实战(植物大战僵尸

目录 1.前言2.外挂类型3.前置知识4.CE查找基质4.1 逐步分析4.2 暴力搜索5.实现数值外挂6.dll导入表注入7.实现行为外挂(无敌类型)8.源码下载与外挂进阶本篇原文为:游戏外挂原理解析:逆向分析与DLL注入实战(植物大战僵尸)。 更多C++进阶、rust、python、逆向等等教程,可…...

【10.10】队列-设计自助结算系统

一、题目 请设计一个自助结账系统&#xff0c;该系统需要通过一个队列来模拟顾客通过购物车的结算过程&#xff0c;需要实现的功能有&#xff1a; get_max()&#xff1a;获取结算商品中的最高价格&#xff0c;如果队列为空&#xff0c;则返回 -1add(value)&#xff1a;将价格为…...

android的ViewModel和LiveData 简介

ViewModel ViewModel 的优势 ViewModel 的替代方案是保存要在界面中显示的数据的普通类。在 activity 或 Navigation 目的地之间导航时&#xff0c;这可能会造成问题。此时&#xff0c;如果您不利用保存实例状态机制存储相应数据&#xff0c;系统便会销毁相应数据。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学习笔记&#xff0c;主要目的是为了帮助后期快速复习使用 学习视频&#xff1a;7小快速通关SpringCloud 辅助文档&#xff1a;SpringCloud快速通关 一、简介 Nacos官网&#xff1a;https://nacos.io/docs/next/quickstart/quick-start/ Nacos /nɑ:kəʊ…...

面试准备——Java理论高级【笔试,面试的核心重点】

集合框架 Java集合框架是面试中的重中之重&#xff0c;尤其是对List、Set、Map的实现类及其底层原理的考察。 1. List ArrayList&#xff1a; 底层是动态数组&#xff0c;支持随机访问&#xff08;通过索引&#xff09;&#xff0c;时间复杂度为O(1)。插入和删除元素时&#…...

AI伴读-清华大学104页《DeepSeek:从入门到精通》

辅助工具&#xff1a;deepseek、豆包AI伴读 官网&#xff1a;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&#xff1a;从入…...

unity学习34:角色相关3,触发器trigger,铰链 hingejoint 等 spring joint, fixed joint

目录 1 触发的实现条件 1.1 碰撞的的实现条件 1.2 触发的实现条件 1.3 触发器trigger&#xff0c;直接拿 碰撞器collider修改下配置即可 2 触发器相关实验&#xff1a;触发开门效果 2.0 目标 2.1 player物体的属性 2.2 新建一个trigger 物体 2.3 新建一个被trigger 控…...

HarmonyOS Next 方舟字节码文件格式介绍

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

计算机视觉语义分割——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 列动态布局

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

DeepSeek开源多模态大模型Janus-Pro部署

DeepSeek多模态大模型部署 请自行根据电脑配置选择合适环境配置安装conda以及gitJanus 项目以及依赖安装运行cpu运行gpu运行 进入ui界面 请自行根据电脑配置选择合适 本人家用电脑为1060&#xff0c;因此部署的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中的数据存储在缓存中&#xff0c;如果没有持久化的策略&#xff0c;Redis一旦宕机&#xff0c;那么将会导致数据丢失&#xff1b;因此redis提供了以下两种持久化方式&#xff1a;RDB和AOF 一般来说&#xff0c;大部分公司对这两种方式都是同时开启的 一、RDB RDB策略全…...

基于Kubernetes Operator的MySQL InnoDB Cluster自动化部署实践

1. MySQL InnoDB Cluster与Kubernetes Operator基础 MySQL InnoDB Cluster是MySQL官方提供的高可用数据库解决方案&#xff0c;它基于MySQL Group Replication技术构建&#xff0c;能够实现多节点数据同步和自动故障转移。想象一下&#xff0c;这就像是一个由多个数据库实例组…...

5分钟学会用AI将手绘草图转为专业科研图表代码

5分钟学会用AI将手绘草图转为专业科研图表代码 【免费下载链接】DeTikZify Synthesizing Graphics Programs for Scientific Figures and Sketches with TikZ 项目地址: https://gitcode.com/gh_mirrors/de/DeTikZify 你是否曾因绘制科研图表而烦恼&#xff1f;面对复杂…...

阿里开源Z-Image镜像体验:ComfyUI可视化生成汉服美女实战

阿里开源Z-Image镜像体验&#xff1a;ComfyUI可视化生成汉服美女实战 1. 开篇&#xff1a;当汉服遇见AI绘画 想象一下&#xff0c;你只需要输入"一位穿着汉服的中国女性站在樱花树下"&#xff0c;AI就能在几秒钟内生成一张细节精致的写实风格图像。这不再是科幻场景…...

从黑客攻防角度看网络命令:如何用ping/tracert/nslookup发现网络安全隐患

网络命令的攻防实战&#xff1a;用基础工具发现隐藏的安全威胁 当大多数人还在把ping、tracert这些基础网络命令当作简单的连通性测试工具时&#xff0c;安全工程师已经将它们变成了发现网络威胁的"显微镜"。这些看似简单的命令行工具&#xff0c;在专业的安全分析场…...

MediaPipe模型优化:从性能瓶颈到实时推理的全流程解决方案

MediaPipe模型优化&#xff1a;从性能瓶颈到实时推理的全流程解决方案 【免费下载链接】mediapipe Cross-platform, customizable ML solutions for live and streaming media. 项目地址: https://gitcode.com/GitHub_Trending/med/mediapipe 问题发现&#xff1a;计算机…...

GodotPckTool 终极指南:如何在命令行中高效管理Godot游戏资源包

GodotPckTool 终极指南&#xff1a;如何在命令行中高效管理Godot游戏资源包 【免费下载链接】GodotPckTool Standalone tool for extracting and creating Godot .pck files 项目地址: https://gitcode.com/gh_mirrors/go/GodotPckTool 你是否曾经需要在不启动Godot引擎…...

工程仿真平台OpenRocket:从物理试验到数字孪生的技术跃迁

工程仿真平台OpenRocket&#xff1a;从物理试验到数字孪生的技术跃迁 【免费下载链接】openrocket Model-rocketry aerodynamics and trajectory simulation software 项目地址: https://gitcode.com/GitHub_Trending/op/openrocket 在现代工程设计领域&#xff0c;物理…...

COMSOL 6.1 激光粉末床熔融气孔缺陷演化仿真:开启微观世界的探索之旅

COMSOL 6.1 激光粉末床熔融气孔缺陷演化仿真案例模型 本案例选用层流和流体传热模块&#xff0c;采用水平集法&#xff0c;考虑材料的热物性以及激光加工过程中的马兰戈尼效应、熔融金属表面张力、反冲压力、相变潜热、热对流和热辐射&#xff0c;建立含气孔缺陷的二维数值仿真…...

2025.12晶晨S905L3S-L3SB安卓9通刷实战:当贝桌面加持,解锁多品牌盒子新玩法

1. 晶晨S905L3S-L3SB通刷包的前世今生 第一次听说晶晨S905L3S-L3SB芯片能通刷时&#xff0c;我正对着家里三台不同品牌的电视盒子发愁。这些盒子有的来自运营商赠送&#xff0c;有的是二手市场淘来的&#xff0c;虽然硬件配置相近&#xff0c;但系统体验天差地别。直到发现这个…...

终极指南:如何构建现代化微服务架构 - Zend Framework Expressive完整教程

终极指南&#xff1a;如何构建现代化微服务架构 - Zend Framework Expressive完整教程 【免费下载链接】zendframework Official Zend Framework repository 项目地址: https://gitcode.com/gh_mirrors/ze/zendframework 在当今快速发展的微服务架构时代&#xff0c;PHP…...