当前位置:网站首页>Solving Hanoi Tower problem
Solving Hanoi Tower problem
2022-06-28 13:49:00 【Hua Weiyun】
Solving the tower of Hanoi problem
【 Common topics 】
Title Description :
Given a by n A tower of disks , These disks are sleeved on the first column in a decreasing way . Now move the whole tower to the third column , Only one disc can be moved at a time , And the larger disc cannot be placed on the smaller disc during movement .
Input :
Enter only one positive integer n
Output :
Next, each line outputs one move step .
Here it is , We discuss a simple and well understood approach —— Recursive approach , Since recursion , We just have to think about recursive functions , Let's leave the rest to the function .
【 Code 】
package pers.keafmd.accumulate.codeinterviewguide.hanoi;import java.util.ArrayList;import java.util.List;/** * Keafmd * * @ClassName: ClassicTowerOfHanoi * @Description: Classic Hanoi Tower * @author: Cowherd Conan * @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(" The first "+step+++" Step , hold "+a.get(0)+" Number from "+a+" Move to "+c); c.add(a.remove(a.size()-1)); return; } // first a It's full , All the dishes are here a On ,b It's empty. ,c It's empty. // Three step strategy : // First step : hold a above n-1 A plate from a With the help of c Move to b On move(n-1,a,c,b); System.out.println("a:"+a+",b:"+b+",c:"+c); System.out.println(" The first "+step+++" Step , hold "+a.get(a.size()-1)+" Number from "+a+" Move to "+c); // The second step : hold a The bottom plate on the top is from a Move to c c.add(a.remove(a.size()-1)); // The third step : hold b above n-1 With the help of a Move to c On 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); }}The test results :
a:[1, 2, 3, 4],b:[],c:[] The first 1 Step , hold 1 Number from [1, 2, 3, 4] Move to []a:[1, 2, 3],b:[4],c:[] The first 2 Step , hold 3 Number from [1, 2, 3] Move to []a:[4],b:[1, 2],c:[3] The first 3 Step , hold 4 Number from [4] Move to [3]a:[1, 2],b:[3, 4],c:[] The first 4 Step , hold 2 Number from [1, 2] Move to []a:[3, 4],b:[2],c:[1] The first 5 Step , hold 3 Number from [3, 4] Move to [1]a:[3],b:[1, 4],c:[2] The first 6 Step , hold 3 Number from [3] Move to [2]a:[1, 4],b:[],c:[2, 3] The first 7 Step , hold 1 Number from [1, 4] Move to [2, 3]a:[1],b:[2, 3, 4],c:[] The first 8 Step , hold 1 Number from [1] Move to []a:[2, 3, 4],b:[],c:[1] The first 9 Step , hold 2 Number from [2, 3, 4] Move to [1]a:[2, 3],b:[1, 4],c:[] The first 10 Step , hold 3 Number from [2, 3] Move to []a:[1, 4],b:[2],c:[3] The first 11 Step , hold 1 Number from [1, 4] Move to [3]a:[2],b:[3, 4],c:[1] The first 12 Step , hold 2 Number from [2] Move to [1]a:[3, 4],b:[1, 2],c:[] The first 13 Step , hold 3 Number from [3, 4] Move to []a:[3],b:[4],c:[1, 2] The first 14 Step , hold 3 Number from [3] Move to [1, 2]a:[4],b:[],c:[1, 2, 3] The first 15 Step , hold 4 Number from [4] Move to [1, 2, 3]Process finished with exit code 0边栏推荐
- 行动诠释价值,城联优品韩董事长出席广东英德抗洪捐赠公益活动会
- Kubernetes 深入理解kubernetes(一)
- 开源社邀您参加OpenInfra Days China 2022,议题征集进行中~
- How to solve the data inconsistency between redis and MySQL?
- 你的代碼會說話嗎?(上)
- 2022年中国运维安全产品市场规模及发展趋势预测分析
- go数组与切片,[]byte转string[通俗易懂]
- i++ , ++i
- Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"
- Luogu_ P1303 A*B Problem_ High precision calculation
猜你喜欢

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

你的代码会说话吗?(上)

Make an ink screen electronic clock, cool!
![(original) [Maui] realize](/img/76/d79b9cf4ed44870bb20a189315def9.jpg)
(original) [Maui] realize "floating action button" step by step

DevEco Studio 3.0编辑器配置技巧篇

BERT为何无法彻底干掉BM25??

Yii2 writing the websocket service of swoole

Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”

Can your code talk? (upper)

MySQL multi table joint query
随机推荐
(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
仅用递归函数和栈操作逆序一个栈
(original) [Maui] realize "floating action button" step by step
一个bug肝一周...忍不住提了issue
行动诠释价值,城联优品韩董事长出席广东英德抗洪捐赠公益活动会
原生JS 实现页面元素的拖动 拖拽
G : 最大流问题
众昂矿业着眼氟化工产业,布局新能源产业链
How to solve the problem that the computer wireless network does not display the network list
[understanding of opportunity -32]: Guiguzi - Dui [x ī] Five attitudes towards danger and problems
Black apple installation tutorial OC boot "suggestions collection"
Visual design tutorial of word cloud
Jupyter notebook中添加虚拟环境
Kubernetes' in-depth understanding of kubernetes (II) declaring organizational objects
猫狗队列
How fragrant! The most complete list of common shortcut keys for pychar!
Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
单元测试 CI/CD
Jerry's wif interferes with Bluetooth [chapter]