当前位置:网站首页>求解汉诺塔问题
求解汉诺塔问题
2022-06-28 13:46:00 【华为云】
求解汉诺塔问题
【常见题目】
题目描述:
给定一个由n个圆盘组成的塔,这些圆盘按照大小递减的方式套在第一根柱上。现要将整个塔移动到第三根柱上,每次只能移动一个圆盘,且较大的圆盘在移动过程中不能放置在较小的圆盘上面。
输入:
输入只有一个正整数n
输出:
接下来每一行输出一步移动步骤。
在此,我们讨论比较一种简单而且很好理解的做法——递归做法,既然递归,我们只要想好递归函数,其它的就交给函数吧。
【代码】
package pers.keafmd.accumulate.codeinterviewguide.hanoi;import java.util.ArrayList;import java.util.List;/** * Keafmd * * @ClassName: ClassicTowerOfHanoi * @Description: 经典汉诺塔 * @author: 牛哄哄的柯南 * @date: 2022-06-27 19:08 */public class ClassicTowerOfHanoi { public int step = 1; public void hanoi(List<Integer> a,List<Integer> b,List<Integer> c){ int n = a.size(); move(n,a,b,c); } public void move(int n,List<Integer> a,List<Integer> b,List<Integer> c){ if(n == 1){ System.out.println("a:"+a+",b:"+b+",c:"+c); System.out.println("第"+step+++"步,把"+a.get(0)+"号从"+a+"移动到"+c); c.add(a.remove(a.size()-1)); return; } // 最初 a是满的,所有盘子都在a上,b是空的,c是空的 //三步走战略: //第一步:把a上面的n-1个盘子从a借助c移动到b上 move(n-1,a,c,b); System.out.println("a:"+a+",b:"+b+",c:"+c); System.out.println("第"+step+++"步,把"+a.get(a.size()-1)+"号从"+a+"移动到"+c); //第二步:把a上面的最下面的那个盘子从a移动到c c.add(a.remove(a.size()-1)); //第三步:把b上面的n-1个盘子借助a移动到c上 move(n-1,b,a,c); } public static void main(String[] args) { List<Integer> aList = new ArrayList<>(); List<Integer> bList = new ArrayList<>(); List<Integer> cList = new ArrayList<>(); aList.add(1); aList.add(2); aList.add(3); aList.add(4);// A.add(5);// A.add(6); ClassicTowerOfHanoi classicTowerOfHanoi = new ClassicTowerOfHanoi(); classicTowerOfHanoi.hanoi(aList,bList,cList); }}测试效果:
a:[1, 2, 3, 4],b:[],c:[]第1步,把1号从[1, 2, 3, 4]移动到[]a:[1, 2, 3],b:[4],c:[]第2步,把3号从[1, 2, 3]移动到[]a:[4],b:[1, 2],c:[3]第3步,把4号从[4]移动到[3]a:[1, 2],b:[3, 4],c:[]第4步,把2号从[1, 2]移动到[]a:[3, 4],b:[2],c:[1]第5步,把3号从[3, 4]移动到[1]a:[3],b:[1, 4],c:[2]第6步,把3号从[3]移动到[2]a:[1, 4],b:[],c:[2, 3]第7步,把1号从[1, 4]移动到[2, 3]a:[1],b:[2, 3, 4],c:[]第8步,把1号从[1]移动到[]a:[2, 3, 4],b:[],c:[1]第9步,把2号从[2, 3, 4]移动到[1]a:[2, 3],b:[1, 4],c:[]第10步,把3号从[2, 3]移动到[]a:[1, 4],b:[2],c:[3]第11步,把1号从[1, 4]移动到[3]a:[2],b:[3, 4],c:[1]第12步,把2号从[2]移动到[1]a:[3, 4],b:[1, 2],c:[]第13步,把3号从[3, 4]移动到[]a:[3],b:[4],c:[1, 2]第14步,把3号从[3]移动到[1, 2]a:[4],b:[],c:[1, 2, 3]第15步,把4号从[4]移动到[1, 2, 3]Process finished with exit code 0边栏推荐
- How about stock online account opening and account opening process? Is it safe to open a mobile account?
- Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性
- G : 最大流问题
- 单元测试 CI/CD
- Why will the new 5g standard bring lower TCO to the technology stack
- Votre Code parle? (1)
- MySQL multi table joint query
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
- 2022年中国运维安全产品市场规模及发展趋势预测分析
- 公司领导说,个人代码超10个Bug就开除,是什么体验?
猜你喜欢
![(original) [Maui] realize](/img/76/d79b9cf4ed44870bb20a189315def9.jpg)
(original) [Maui] realize "floating action button" step by step

你的代碼會說話嗎?(上)

CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea

Connected to rainwater series problems

Recognize the startup function and find the user entry

如何设计数据可视化平台

Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系

Elastic box auto wrap demo

Introduction to PWN (1) binary Basics

thinkphp6 多级控制器目录访问解决方法
随机推荐
Solution to directory access of thinkphp6 multi-level controller
Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"
The difference between align items and align content
From PDB source code to frame frame object
How to open an account on the flush? Is it safe
CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
30 sets of JSP website source code collection "suggestions collection"
The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
RK3399平台开发系列讲解(使用篇)Pinctrl子系统的介绍 - 视频介绍
做一个墨水屏电子钟,炫酷!
Why will the new 5g standard bring lower TCO to the technology stack
Why do more and more users give up swagger and choose apifox
Double buffer drawing
Luogu_ P1303 A*B Problem_ High precision calculation
2.01 backpack problem
How fragrant! The most complete list of common shortcut keys for pychar!
Inftnews | technology giants accelerate their march into Web3 and metauniverse
Ruijie switch configuration SSH password login command [easy to understand]
Jerry's wif interferes with Bluetooth [chapter]
Zhongang mining focuses on the fluorine chemical industry and lays out the new energy industry chain