Codves 3145 4412 4835 汉诺塔 递归

来源:互联网 发布:如何撤销淘宝投诉 编辑:程序博客网 时间:2024/05/19 14:38

3145 4412 4835
三倍经验。

思路:
1、将 n-1 从 A 移到 B 上;
2、将 n-1 从 B 移到 C 上。

步数: 根据 F(x) = 2*F(x-1) + 1, F(1) = 1;递推。

代码:

#include <iostream>#include <queue>#include <cstring>#include <cstdio>using namespace std;int cnt = 0;int f[1000 + 10];void hanoi(int n, char f, char x, char t){    if(n == 1)    {        printf("%d from %c to %c\n", n, f, t);        return;    }    hanoi(n-1, f, t, x);    printf("%d from %c to %c\n", n, f, t);    hanoi(n-1, x, f, t);}int main(){    int n;    cin >> n;    f[1] = 1;    for(int i = 2; i <= n; i ++)        f[i] = f[i-1]*2 + 1;    cout << f[n] << endl;    hanoi(n,'A','B','C');    return 0;}

这里写图片描述
懵逼,

三倍经验拿的并不开心啊QAQ, 上次做这个题是在 1月, 现在10月反而不会做了,还翻题解,不开心啊QAQ。

0 0
原创粉丝点击