您的位置:首页 > 产品设计 > UI/UE

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: