汉诺塔谜题 递归

看动图: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);
		}
				
	}	

}

 

你可能感兴趣的:(JAVA)