1. 程式人生 > >漢諾塔謎題 遞迴

漢諾塔謎題 遞迴

看動圖: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);
		}
				
	}	

}