Stack_Queue 汉诺塔Hanoi问题 @CareerCup
2014-03-02 04:43
399 查看
原文:
In the classic problem of the Towers of Hanoi, you have 3 rods 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 (e.g., 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 rod onto the next rod.
A disk can only be placed on top of a larger disk.
Write a program to move the disks from the first rod to the last using Stacks
译文:
编程解决汉诺塔问题,使用数据结构栈
经典递归题,非递归解法参考:
http://blog.csdn.net/shaohui/article/details/660018
http://hawstein.com/posts/3.4.html
下面只列举递归解法:
package Stack_Queue;
import java.util.Stack;
public class S3_4 {
static class Tower {
private Stack<Integer> disks;
private int index;
public Tower(int i) {
disks = new Stack<Integer>();
index = i;
}
public int index() {
return index;
}
// 添加一个disk到当前栈
public void add(int disk) {
// 不能把一个大的盘放在一个小的盘上面
if(!disks.isEmpty() && disks.peek() <= disk) {
System.out.println("Error placing disk " + disk);
} else {
disks.push(disk);
}
}
// 把当前栈顶元素移到Tower t的栈顶
public void moveTopTo(Tower t) {
int top = disks.pop();
t.add(top);
}
public void print() {
System.out.println("Contents of Tower " + index() + ": " + disks.toString());
}
// 把n个盘子从当前Tower移到destination Tower,借助bufferTower
public void moveDisks(int n, Tower destination, Tower buffer){
if (n > 0) {
String tag = "move_" + n + "_disks_from_" + this.index + "_to_" + destination.index + "_with_buffer_" + buffer.index;
System.out.println("<" + tag + ">");
moveDisks(n - 1, buffer, destination); // 先把最上面的n-1个盘借助destination Tower移到buffer Tower
System.out.println("<move_top_from_" + this.index + "_to_" + destination.index + ">");
System.out.println("<before>");
System.out.println("<source_print>");
this.print();
System.out.println("</source_print>");
System.out.println("<destination_print>");
destination.print();
System.out.println("</destination_print>");
System.out.println("</before>");
moveTopTo(destination); // 然后把当前Tower的最底层一个盘直接移到destination上
System.out.println("<after>");
System.out.println("<source_print>");
this.print();
System.out.println("</source_print>");
System.out.println("<destination_print>");
destination.print();
System.out.println("</destination_print>");
System.out.println("</after>");
System.out.println("</move_top_from_" + this.index + "_to_" + destination.index + ">");
buffer.moveDisks(n - 1, destination, this); // 最后借助当前Tower,把在buffer Tower的n-1个盘移到destination Tower
System.out.println("</" + tag + ">");
}
}
}
public static void main(String[] args) {
// Set up code.
int n = 5;
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);
}
// Copy and paste output into a .XML file and open it with internet explorer.
//towers[0].print();
towers[0].moveDisks(n, towers[2], towers[1]);
//towers[2].print();
}
}
In the classic problem of the Towers of Hanoi, you have 3 rods 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 (e.g., 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 rod onto the next rod.
A disk can only be placed on top of a larger disk.
Write a program to move the disks from the first rod to the last using Stacks
译文:
编程解决汉诺塔问题,使用数据结构栈
经典递归题,非递归解法参考:
http://blog.csdn.net/shaohui/article/details/660018
http://hawstein.com/posts/3.4.html
下面只列举递归解法:
package Stack_Queue;
import java.util.Stack;
public class S3_4 {
static class Tower {
private Stack<Integer> disks;
private int index;
public Tower(int i) {
disks = new Stack<Integer>();
index = i;
}
public int index() {
return index;
}
// 添加一个disk到当前栈
public void add(int disk) {
// 不能把一个大的盘放在一个小的盘上面
if(!disks.isEmpty() && disks.peek() <= disk) {
System.out.println("Error placing disk " + disk);
} else {
disks.push(disk);
}
}
// 把当前栈顶元素移到Tower t的栈顶
public void moveTopTo(Tower t) {
int top = disks.pop();
t.add(top);
}
public void print() {
System.out.println("Contents of Tower " + index() + ": " + disks.toString());
}
// 把n个盘子从当前Tower移到destination Tower,借助bufferTower
public void moveDisks(int n, Tower destination, Tower buffer){
if (n > 0) {
String tag = "move_" + n + "_disks_from_" + this.index + "_to_" + destination.index + "_with_buffer_" + buffer.index;
System.out.println("<" + tag + ">");
moveDisks(n - 1, buffer, destination); // 先把最上面的n-1个盘借助destination Tower移到buffer Tower
System.out.println("<move_top_from_" + this.index + "_to_" + destination.index + ">");
System.out.println("<before>");
System.out.println("<source_print>");
this.print();
System.out.println("</source_print>");
System.out.println("<destination_print>");
destination.print();
System.out.println("</destination_print>");
System.out.println("</before>");
moveTopTo(destination); // 然后把当前Tower的最底层一个盘直接移到destination上
System.out.println("<after>");
System.out.println("<source_print>");
this.print();
System.out.println("</source_print>");
System.out.println("<destination_print>");
destination.print();
System.out.println("</destination_print>");
System.out.println("</after>");
System.out.println("</move_top_from_" + this.index + "_to_" + destination.index + ">");
buffer.moveDisks(n - 1, destination, this); // 最后借助当前Tower,把在buffer Tower的n-1个盘移到destination Tower
System.out.println("</" + tag + ">");
}
}
}
public static void main(String[] args) {
// Set up code.
int n = 5;
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);
}
// Copy and paste output into a .XML file and open it with internet explorer.
//towers[0].print();
towers[0].moveDisks(n, towers[2], towers[1]);
//towers[2].print();
}
}
相关文章推荐
- Stack_Queue 猫狗队列问题 Cat dog queue @CareerCup
- Stack_Queue 一个数组实现三个栈 @CareerCup
- Stack_Queue 两个栈实现一个队列 @CareerCup
- Stack_Queue 把栈排序 Sort a stack @CareerCup
- Stack_Queue 栈实现min函数 @CareerCup
- 【C#复习总结】探究各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字
- CareerCup Implement a stack that pops out the most frequently added item
- Recursion 爬楼梯问题 @CareerCup
- Recursion 硬币组合问题 @CareerCup
- Recursion 叠箱子最高问题 @CareerCup
- 第三节:深度剖析各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字
- Recursion 八皇后问题 @CareerCup
- Recursion 二维空间里机器人向右或向下走的所有路径问题 @CareerCup
- CareerCup之1.8 字符串移位包含问题
- 问题 D: Stack ? Queue ?
- ndk-stack定位不出崩溃代码行的问题
- ZOJ 3210 A Stack or A Queue
- 关于NSOperationQueue,一个容易让初学者误解的问题
- 请使劲回答一个关于UNIX/Linux自动扩展stack的问题
- Moderate 找最小排序区间 @CareerCup