Stack_Queue 汉诺塔Hanoi问题 @CareerCup

原文:

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();
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值