输入数字N代表有多少块盘子需要移动,将盘子从A移动到C,可以把B作为辅助柱子。
输出移动步骤。
import java.util.*;
public class HannuoTa {
static List<String> results = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
hanluota(n,"a","c","b");
for (String s:results){
System.out.println(s);
}
}
public static void hanluota(int n,String left,String right,String mid){
if(n == 1){
results.add(left+"->"+right);
}else {
hanluota(n-1,left,mid,right);
hanluota(1,left,right,mid);
hanluota(n-1,mid,right,left);
}
}
}