URAL1225——DP——Flags

Description

On the Day of the Flag of Russia a shop-owner decided to decorate the show-window of his shop with textile stripes of white, blue and red colors. He wants to satisfy the following conditions:
  1. Stripes of the same color cannot be placed next to each other.
  2. A blue stripe must always be placed between a white and a red or between a red and a white one.
Determine the number of the ways to fulfill his wish.
Example. For N = 3 result is following:
Problem illustration

Input

N, the number of the stripes, 1 ≤ N ≤ 45.

Output

M, the number of the ways to decorate the shop-window.

Sample Input

inputoutput
3
4
 大意:蓝色必须在红白之间,对于动态规划,只要考虑当前状态是怎么来的,当前状态可以由n-1转移过来,红和白,也可以由n-2转移过来(n-1为蓝色固定好了)红蓝,白蓝,把蓝色看成是与白红相关的一个整体,不是单独的一个元素
所以得到  f[n] = f[n-1] + f[n-2]
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    long long dp[50];
    int n;
    while(~scanf("%d",&n)){
        dp[1] = 2;
        dp[2] = 2;
        for(int i = 3; i <= n; i++)
            dp[i] = dp[i-1] + dp[i-2];
        printf("%lld
",dp[n]);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zero-begin/p/4482909.html