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

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

void move(int i, int from, int to){
  printf("move %d from %d to %d\n", 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;

相关文章:

  • 2022-02-28
  • 2022-02-10
  • 2022-12-23
  • 2021-12-25
  • 2021-08-17
  • 2021-12-28
  • 2021-04-29
  • 2021-12-20
猜你喜欢
  • 2021-07-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2021-10-24
  • 2021-12-19
相关资源
相似解决方案