当前位置:网站首页>Tower of Hanoi problem
Tower of Hanoi problem
2022-07-31 01:48:00 【Xiao Lu wants to brush the force and deduct the question】
前言
给定一个数组arr,长度为N,arr中的值只有1,2,3三种
arr[i] == 1,代表汉诺塔问题中,从上往下第i个圆盘目前在左
arr[i] == 2,代表汉诺塔问题中,从上往下第i个圆盘目前在中
arr[i] == 3,代表汉诺塔问题中,从上往下第i个圆盘目前在右
那么arr整体就代表汉诺塔游戏过程中的一个状况
如果这个状况不是汉诺塔最优解运动过程中的状况,返回-1
如果这个状况是汉诺塔最优解运动过程中的状况,返回它是第几个状况
解题思路
7A state of the Layer Tower of Hanoi problem
The first state of the optimal solution
All plates are on the left
The second state is1Plate No. is on the right
The third state is2The plate is in the middle
i: 1~iThe disc needs to be moved
F: 1~iOn what disk is the disk of ,Possibly left-center-right.
t:The place to go may be left,中,右
other:除了from, toanother location
iThe layer of the disc doesn't make any sense at allother.上
如果index还在From上,Explain that the first big step is not over
第一步:1~i-1 从from到other
第二步:i从from到to
第三步:1~i-1从other到to
Break down each step with recursion
最后相加,get the first step
代码
public static int kth(int[] arr) {
int N = arr.length;
return step(arr, N - 1, 1, 3, 2);
}
// 0...index这些圆盘,arr[0..index] index+1层塔
// 在哪?from 去哪?to 另一个是啥?other
// arr[0..index]这些状态,是index+1层汉诺塔问题的,最优解第几步
public static int step(int[] arr, int index, int from, int to, int other) {
if (index == -1) {
return 0;
}
if (arr[index] == other) {
return -1;
}
// arr[index] == from arr[index] == to;
if (arr[index] == from) {
return step(arr, index - 1, from, other, to);
} else {
int p1 = (1 << index) - 1;
int p2 = 1;
int p3 = step(arr, index - 1, other, to, from);
if (p3 == -1) {
return -1;
}
return p1 + p2 + p3;
}
}
边栏推荐
- Inner monologue from a female test engineer...
- 内网渗透——提权
- Chi-square distribution of digital image steganography
- Fiddler抓包模拟弱网络环境测试
- leetcode-952: Calculate max component size by common factor
- 力扣刷题之有效的正方形(每日一题7/29)
- 项目开发软件目录结构规范
- keep-alive缓存组件
- pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
- Simple confession page
猜你喜欢
What are the project management tools like MS Project
内网渗透——提权
1.非类型模板参数 2.模板的特化 3.继承讲解
Meta元宇宙部门第二季度亏损28亿 仍要继续押注?元宇宙发展尚未看到出路
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
数字图像隐写术之JPEG 隐写分析
"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition
leetcode-1161: Maximum in-layer element sum
Xiaohei's leetcode journey: 104. The maximum depth of a binary tree
Chi-square distribution of digital image steganography
随机推荐
MySql的安装配置超详细教程与简单的建库建表方法
Can an inexperienced college graduate switch to software testing?my real case
Gateway路由的配置方式
程序员转正述职报告/总结
手把手教你配置Jenkins自动化邮件通知
1782. 统计点对的数目 双指针
pc端判断当前使用浏览器类型
JPEG Steganalysis of Digital Image Steganography
934. 最短的桥
基于FPGA的售货机
一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例
Are you still working hard on the limit of MySQL paging?
Shell变量与赋值、变量运算、特殊变量
MySql installation and configuration super detailed tutorial and simple method of building database and table
数字图像隐写术之卡方分布
Inner monologue from a female test engineer...
cudaMemcpy学习笔记
leetcode-128:最长连续序列
蛮力法/邻接表 广度优先 有向带权图 无向带权图
1.非类型模板参数 2.模板的特化 3.继承讲解