HDU 2045 不容易系列之(3)―― LELE的RPG难题(递推)

题意:有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法. 

题解:本来当n=1时,答案是0的(首尾不同时不可能的),但是这儿答案是3

接着我们可以这样来想

当n=2时答案是6

当n>2时,我们等于前一个(dp[i-1])的个数加上,最后一位有两种可能(固定第i-1位不变时)。所以就是 dp[i]=dp[i-1]+2*dp[i-2](i>2)

 

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=1<<28;
const double Pi=acos(-1.0);
const int Mod=1e9+7;
const int Max=100010;
map<int,int> mp;
ll dp[150];
void Init(int n)
{
    dp[1]=3ll;
    dp[2]=dp[3]=6ll;
    for(int i=4;i<n;++i)
        dp[i]=2*dp[i-2]+dp[i-1];
    return;
}
int main()
{
    std::ios::sync_with_stdio(false);
    int t,n,m;
    Init(51);
    while(cin >> n)
    {
    cout << dp[n] << endl;
    }
    return 0;
}

 

posted @ 2016-09-29 23:09  专注如一  阅读(374)  评论(0编辑  收藏  举报