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



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