汉诺塔问题(递归之路)

经典递归问题----汉诺塔问题

#include <stdio.h>
#include <stdlib.h>

void move(int i, int from, int to){
  printf("move %d from %d to %d ", i, from, to);
}
void hanoi(int n, int from, int help, int to){ //use 'help' to move 'from' to 'to'.
  if(n == 1){
    move(n, from, to);
  }else{
    hanoi(n-1, from, to, help);
    move(n, from, to);
    hanoi(n-1, help, from, to);
  }
}

复杂度分析:T(n) = 2T(n-1)+1 ==> T(n)=2^n-1;

原文地址:https://www.cnblogs.com/informatics/p/5093425.html