当前位置:网站首页>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边栏推荐
- Recognize the startup function and find the user entry
- Unit test ci/cd
- Analysis and processing of GPS data format [easy to understand]
- 2022年中国运维安全产品市场规模及发展趋势预测分析
- 再谈exception——异常抛出时会发生什么?
- 接雨水系列问题
- Pytorch Foundation
- How to solve the problem that the computer wireless network does not display the network list
- [experience sharing] summary of database operations commonly used in Django development
- 再談exception——异常拋出時會發生什麼?
猜你喜欢

2022年中国运维安全产品市场规模及发展趋势预测分析

DevEco Studio 3.0编辑器配置技巧篇

Pytorch model

PostgreSQL surpasses MySQL

欧拉恒等式:数学史上的真正完美公式!

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

程序员坐牢了,会被安排去写代码吗?

Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"

PHP crawls web pages for specific information

Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event
随机推荐
《蛤蟆先生去看心里医生》阅读笔记
i++ , ++i
The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
Npoi export excel and download to client
为什么新的5G标准将为技术栈带来更低的 TCO
其他国产手机未能填补华为的空缺,苹果在高端手机市场已无对手
Make an ink screen electronic clock, cool!
Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”
First knowledge of exception
Idea global search shortcut settings
Build a learning environment
Zhongang mining focuses on the fluorine chemical industry and lays out the new energy industry chain
排序
Oracle cloud infrastructure extends distributed cloud services to provide organizations with greater flexibility and controllability
MySQL multi table joint query
单元测试 CI/CD
1015. picking flowers
3、项目的整体UI架构
2022年中国运维安全产品市场规模及发展趋势预测分析
(original) [Maui] realize "floating action button" step by step