汉诺威塔

var move = (n, from , to) => {console.log(`盘${n} ${from} - ${end}`)}

var hanio = (n, start, trans, end) => {
  if(n===1){
     move(n, start, end);
   }else{
    hanio(n-1, start, end, trans); // 只要把 n-1盘先移动到 trans 柱
    move(n, start, end); // 第n 从 start柱 移动到 end 柱
    hanio(n-1, trans, start, end); // 再把n-1盘 移到end 柱
   }


}
hanio(5, 'A', 'B', 'C') // 从A 移到C 

Leave a Reply

Your email address will not be published. Required fields are marked *