227. Mock Hanoi Tower by Stacks【LintCode java】
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:
- Only one disk can be moved at a time.
- A disk is slid off the top of one tower onto the next tower.
- 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]
*/
227. Mock Hanoi Tower by Stacks【LintCode java】的更多相关文章
- 227. Mock Hanoi Tower by Stacks【easy】
In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different sizes which ca ...
- Lintcode227 Mock Hanoi Tower by Stacks solution 题解
[题目描述] In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different sizes w ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
随机推荐
- [LuoguP2900] [USACO08MAR]土地征用(Land Acquisition)
土地征用 (Link) 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地.如果约翰单买一块土 地,价格就是土地的面积.但他可以选择并购一组土地,并购的价格为这些土地中最大的长 乘以最大的宽.比 ...
- Android 第三方应用接入微信平台研究情况分享
微信平台开放后倒是挺火的,许多第三方应用都想试下接入微信这个平台,毕竟可以利用微信建立起来的关系链来拓展自己的应用还是挺不错的 最近由于实习需要也在研究这个东西,这里把我的整个研究情况给出来 微信平台 ...
- Spring - 父容器与子容器
一.Spring容器(父容器) 1.Mapper代理对象 2.Service对象 二.Springmvc(前端控制器)(子容器) Controller对象 1.标准的配置是这样的:Con ...
- el表达式不显示值
1.场景是自己搭建一个ssm的项目,登录页面跳转到首页,首页显示登录用户的信息,用request传递的值,用el表达式在jsp页面中没有显示 2.解决办法 早jsp的代码中添加头<%@ page ...
- 基于java的简易计算器实现
方法: 1.将string类型的表达式输入转换成后缀表达式 2.计算后缀表达式 步骤一:将string类型的表达式输入转换成后缀表达式 输入字符串表达式,并将表达式转换成char型数组 String ...
- dva框架使用详解及Demo教程
dva框架的使用详解及Demo教程 在前段时间,我们也学习讲解过Redux框架的基本使用,但是有很多同学在交流群里给我的反馈信息说,redux框架理解上有难度,看了之后还是一脸懵逼不知道如何下手,很多 ...
- Error creating bean with name 'mapper' defined in class path resource [applicationcontext.xml]: Cannot resolve reference to bean 'factory' while setting bean property 'sqlSessionFactory'; nested excep
Error creating bean with name 'mapper' defined in class path resource [applicationcontext.xml]: Cann ...
- Linux 学习第三天
一.常用命令 1.diff A.diff -q 源文件 目标文件 (快速比较文件是否相同) 2.ifconfig.nmcli (查看配置信息) 命令输入注意: Windows 查看网卡配置信息输入命 ...
- Dubbo 安装监控中心
一.Dubbo 安装服务管理控制台 1.在官方Github下载Dubbo OPS 2.下载incubator-dubbo-ops源码后,解压修改配置文件Zookeeper注册中心地址 3.maven打 ...
- IDEA中使用单元测试@Test等,提示没有 Junit.jar包
1.File-->Project Structure-->Modules-->右侧Dependencies-->+号-->JARs or directories... 2 ...