import java.util.Scanner;
/**
* 汉诺塔
*
* @author JayChang
*
*/
public class HanoiResolve {
/**
* 移动位置
*
* @param positionA
* @param positionB
*/
public static void move(String positionA, String positionB) {
System.out.println(positionA + " --> " + positionB);
}
/**
* 汉诺塔递归算法
*
* @param num
* @param positionA
* @param positionB
* @param positionC
*/
public static void hanoi(int num, String positionA, String positionB,
String positionC) {
if (num == 1) {
move(positionA, positionC);
} else {
hanoi(num - 1, positionA, positionC, positionB);
move(positionA, positionC);
hanoi(num - 1, positionB, positionA, positionC);
}
}
public static void main(String[] args) {
int num = new Scanner(System.in).nextInt();
hanoi(num, "PositionA", "PositionB", "PositionC");
}
}
来源:oschina
链接:https://my.oschina.net/u/1167421/blog/546447