Java解决Hanoi Tower(河内塔)问题

/**	Java解决Hanoi Tower(河内塔)问题
 * 	by puladiao
 * 	http://blog.verypod.com
 */
public class HanoiTower {

	public static void main(String[] args) {
		HanoiTower ht = new HanoiTower();
		ht.doMove(3, "a", "b", "c");
	}

	public void doMove(int number, String f, String i, String t) {
		if (number == 1) {
			System.out.printf("Move 1 from %s to %s\n", f, t);
		} else {
			doMove(number-1,f,t,i);
			System.out.printf("Move %d from %s to %s\n", number,f,t);
			doMove(number-1,i,f,t);
		}
	}
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注