有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法.
以上就是著名的RPG难题.
如果你是Cole,我想你一定会想尽办法帮助LELE解决这个问题的;如果不是,看在众多漂亮的痛不欲生的Cole女的面子上,你也不会袖手旁观吧?
1 2设n个格子的颜色数为f[n]。 很容易算出f[1], f[2], f[3]. 假设第n-1个格子的颜色与第1个格子的颜色不相同,那么第n个格子的颜色只能取一种颜色,也就是f[n-1]的个数。 假设相同,那么可以取两种颜色,两种的个数都是f[n-2]。 那么f[n] = f[n-1]+2*f[n-2]。
#include#include #include #include #include #include #include #include #define lson o<<1, l, m #define rson o<<1|1, m+1, r using namespace std; typedef long long LL; const int maxn = 500050; const int mod = 99999997; const int MAX = 0x3f3f3f3f; int t, a, b; LL f[100]; int main() { f[1] = 3; f[2] = 6; f[3] = 6; for(int i = 4; i <= 50; i++) f[i] = f[i-1] + 2*f[i-2]; while(~scanf("%d", &b)) { printf("%I64d\n", f[b]); } return 0; }
??