Description

In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (i.e., each disk sits on top of an even larger one). You have the following constraints:

  1. Only one disk can be moved at a time.
  2. A disk is slid off the top of one tower onto the next tower.
  3. A disk can only be placed on the top of a larger disk.

Write a program to move the disks from the first tower to the last using stacks.

解题:本题是经典的汉诺塔问题,只要想好移动的顺序,还是挺简单的。分为三个塔:原塔(初始化n个盘子),目标塔,缓冲塔。思路是,借助目标塔(此时目标塔充当缓冲塔),先把前n-1个盘子移动到缓冲塔。然后把最后一个移动到目标塔。接着,借助原塔,把缓冲塔的盘子移动到目标塔,递归执行。代码如下:

public class Tower {
private Stack<Integer> disks;
/*
* @param i: An integer from 0 to 2
*/
public Tower(int i) {
// create three towers
disks = new Stack<Integer>();
} /*
* @param d: An integer
* @return: nothing
*/
public void add(int d) {
// Add a disk into this tower
if (!disks.isEmpty() && disks.peek() <= d) {
System.out.println("Error placing disk " + d);
} else {
disks.push(d);
}
} /*
* @param t: a tower
* @return: nothing
*/
public void moveTopTo(Tower t) {
// Move the top disk of this tower to the top of t.
int temp = this.getDisks().pop();
t.add(temp);
} /*
* @param n: An integer
* @param destination: a tower
* @param buffer: a tower
* @return: nothing
*/
public void moveDisks(int n, Tower destination, Tower buffer) {
// Move n Disks from this tower to destination by buffer tower
if(n == 1){
this.moveTopTo(destination);
}else{
this.moveDisks(n-1 , buffer, destination);
this.moveTopTo(destination);
buffer.moveDisks(n-1, destination, this);
} } /*
* @return: Disks
*/
public Stack<Integer> getDisks() {
// write your code here
return disks;
}
} /**
* Your Tower object will be instantiated and called as such:
* Tower[] towers = new Tower[3];
* for (int i = 0; i < 3; i++) towers[i] = new Tower(i);
* for (int i = n - 1; i >= 0; i--) towers[0].add(i);
* towers[0].moveDisks(n, towers[2], towers[1]);
* print towers[0], towers[1], towers[2]
*/

最新文章

  1. JavaScript中对象的含义与this的指向
  2. .Net魔法堂:开启IIS的WebGarden、WebFarm和StateServer之旅
  3. WIN 下的超动态菜单(一)
  4. vm设置静态ip
  5. 如何让静态库中的可执行程序不调用的函数不链接进该可执行程序?(-ffunction-sections -Wl,--gc-sections)
  6. php 异常捕获
  7. eclipse failed to create the java virtual machine 问题图文解析(转)
  8. 【HighCharts系列教程】四、颜色属性——colors
  9. 在windows上搭建ipv6代理
  10. Java企业微信开发_09_素材管理之下载微信临时素材到本地服务器
  11. 使用map做数组与链表去重
  12. AC的故事大结局山寨版(下)(最大流)
  13. Markdown初使用
  14. 第十二节: 总结Quartz.Net几种部署模式(IIS、Exe、服务部署【借助TopSelf、服务类】)
  15. Django model中的class Meta详解
  16. Windows LTSC、LTSB、Server 安装 Windows Store 应用商店
  17. 通过mysql-proxy映射外网访问内网数据库
  18. win10企业版2016长期服务版本激活
  19. Set和WeakSet数据结构
  20. NOI2019冬令营报到通知

热门文章

  1. 浅谈DB2的四个隔离级别
  2. Relay GraphQL理解
  3. 通用输入输出端口 - GPIO
  4. Oracle创建表、修改表、删除表、约束条件语法
  5. Ubuntu18.04安装完应该做的一些事 显卡驱动安装和cuda8.0
  6. AJAX 动态加载后台数据 绑定select
  7. Python学习笔记:第3天 字符串的操作
  8. rtsp over tcp并设置多个options
  9. 【转载++】C/C++错误分析errno,perror,strerror和GetLastError()函数返回的错误代码的意义
  10. Python3爬虫(十四) 验证码处理