人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即"可乐"),经过多方打探,某资深Cole终于知道了原因,原来,LELE最近研究起了著名的RPG难题:
有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法.
以上就是著名的RPG难题.
如果你是Cole,我想你一定会想尽办法帮助LELE解决这个问题的;如果不是,看在众多漂亮的痛不欲生的Cole女的面子上,你也不会袖手旁观吧?
Input
输入数据包含多个测试实例,每个测试实例占一行,由一个整数N组成,(0<n<=50)。
Output
对于每个测试实例,请输出全部的满足要求的涂法,每个实例的输出占一行。
Sample Input
1
2
Sample Output
3
6
题解:这种问题只需考虑最后一个位置(n)与倒数第二个位置(n-1),考虑倒数第二个位置时,如果他和第一个元素不一样,那么你有dp[n-1]种选择,如果和第一个元素一样,因为(n-1)个元素和第1个元素是同种颜色,所以有dp[n-2]个选择,又因为最后一个元素可以选择除了与第一个元素相同以外的元素,有两种,所以最终有2*dp[n-2]种选择,所以得出dp[n]=dp[n-1]+dp[n-2]*2。
代码如下:
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 10007
#define N 107
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
using namespace std;
typedef long long ll;
int main()
{
long long int dp[61];
dp[1]=3;dp[2]=6;dp[3]=6;//3长度与2长度排列相同
for(int i=4; i<=50; i++)
dp[i]=dp[i-1]+2*dp[i-2];
int n;
while(cin>>n)
{
cout<<dp[n]<<endl;
}
return 0;
}