汉诺塔谜题 递归

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mygodhome/article/details/86325245

看动图:https://www.cnblogs.com/tgycoder/p/6063722.html

源:a, 

目标: b

aux: c

public class HiValen {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String A = "A";
		String B = "B";
		String C = "C";
		Towers(4,  A,  B, C);
		
	}
	
	
	public static void Towers(int n, String a, String b, String c ) {
		if (n == 1) {
			System.out.println("Move disk 1 from peg " + a + " to Peg " + b);
		} else {
			Towers(n-1, a, c, b);
			System.out.println("Move disk from peg " + a + " to Peg " + b);
			Towers(n-1, c, b, a);
		}
				
	}	

}

猜你喜欢

转载自blog.csdn.net/mygodhome/article/details/86325245