hdu 2077 汉诺塔IV

http://acm.hdu.edu.cn/showproblem.php?pid=2077

Problem Description
还记得汉诺塔III吗?他的规则是这样的:不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到小盘的上面。xhd在想如果我们允许最大的盘子放到最上面会怎么样呢?(只允许最大的放在最上面)当然最后需要的结果是盘子从小到大排在最右边。
 

Input
输入数据的第一行是一个数据T,表示有T组数据。
每组数据有一个正整数n(1 <= n <= 20),表示有n个盘子。
 

Output
对于每组输入数据,最少需要的摆放次数。
 

Sample Input
2 1 10
 

Sample Output
2 19684
 



#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int a[22]={0,1};

void Init(){
    for(int i=2;i<=20;i++)
        a[i]=3*a[i-1]+1;
}

int main(){

    //freopen("input.txt","r",stdin);

    int t,n;
    scanf("%d",&t);
    Init();
    while(t--){
        scanf("%d",&n);
        printf("%d\n",2*a[n-1]+2);
    }
    return 0;
}



#include <cstdio>
#include <cstring>
int dp[22][2];
int main()
{
    dp[0][0] = dp[0][1] = 0;
    for (int i=1; i<=20; i++){
        dp[i][0] = 2*dp[i-1][0]+dp[i-1][1]+1;
        dp[i][1] = dp[i-1][0]+2*dp[i-1][1]+1;
    }
    int t, n;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n);
        printf("%d\n",dp[n-1][0]+dp[n-1][1]+2);
    }
    return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=2077



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章