码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2077

时间:2017-12-06 14:20:38      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:ring   def   fine   div   amp   name   cpp   ios   efi   

  嗯没错,最近和汉诺塔有仇,继续水一波汉诺塔IV

#include<iostream>
#include<cstring>

#define LL long long
#define N 30

using namespace std;

LL f[4][4][4][N];

LL solve(int a,int b,int c,int n,int &all){
	if(n == 0) return 0;

    if(f[a][b][c][n]) return f[a][b][c][n];

    if(n == all){
        return f[a][b][c][n] = solve(a,c,b,n-1,all) + 2 + solve(c,b,a,n-1,all);
    }
    else if(n == 1){
        if(a == 2 || b == 2) return f[a][b][c][n] = 1;
        else return f[a][b][c][n] = 2;
    }
    else if(a == 2){
        return f[a][b][c][n] = solve(a,c,b,n-1,all) + 1 + solve(c,a,b,n-1,all) + solve(a,b,c,n-1,all);
    }
    else if(b == 2){
        return f[a][b][c][n] = solve(a,b,c,n-1,all) + solve(b,c,a,n-1,all) + 1 + solve(c,b,a,n-1,all);
    }
    else{
        return f[a][b][c][n] = solve(a,c,b,n-1,all) + solve(c,b,a,n-1,all) + 1 + solve(b,c,a,n-1,all) + solve(c,a,b,n-1,all) + 1 + solve(a,c,b,n-1,all) + solve(c,b,a,n-1,all);
    }
}

int main(){
    int n,T;
    cin >> T;
    while(T--){
        cin >> n;
        cout << solve(1,3,2,n,n) << endl;
        memset(f,0,sizeof(f));
    }
    return 0;
}

  感觉和III没啥区别嗯。。。

HDU2077

标签:ring   def   fine   div   amp   name   cpp   ios   efi   

原文地址:http://www.cnblogs.com/woodenhead/p/7992172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!