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

hdu2045 题目链接

题意:

一串 方格,每个格子可以涂三种颜色,要求相邻的格子颜色不能相同,首尾格子也不能相同。

思路:递推。没推出来23333 我好菜啊.jpg.

考虑有n个格子。。那么假设第n-1个格子的颜色是a,根据第一个格子和第n-1个格子颜色是否相同分为两种情况。

如果不相同,那么第n个格子不能和第1个格子颜色相同,也不能和第n-1个格子颜色相同,所以只有一种选择。

如果相同,那么第n个格子有两种选择。

/* ***********************************************
Author :111qqz
Created Time :2016年07月27日 星期三 14时53分16秒
File Name :code/hdu/2045.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=55;
 7LL f[N];
 8int n;
 9int main()
10{
11	#ifndef  ONLINE_JUDGE 
12	freopen("code/in.txt","r",stdin);
13  #endif
1	f[1] = 3;
2	f[2] = 6;
3	f[3] = 6;
4	f[4] = 18;
	for ( int i = 5 ; i < N ; i++)
	     f[i] = f[i-1] + 2LL*f[i-2];
1	while (~scanf("%d",&n))
2	{
3	    printf("%lld\n",f[n]);
4	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}