1225. FlagsTime limit:1.0 Second
Memory limit:64 MBOn the day of the "the Flag of Russia a shop-owner decided to decorate the show-window of the" with textile stripes of White, blue and red colors. He wants to satisfy the following conditions:
- Stripes of the same color cannot is placed next to all other.
- A blue stripe must always being 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:Input
n, the number of the stripes, 1≤
N ≤45.Output
M, the number of the ways to decorate the Shop-window.Sample
1 Stands for White
2. Blue
3. Red
DP[1][J] is selected to the first I stripe, the first I is J this color
and then recursion.
Attention:
1. To use a long long
2. Blue can not be placed on the last and the front, only in the middle
1#include <iostream>2#include <cstring>3 4 using namespacestd;5 6 Const intmaxn= -;7 8 Long Longdp[maxn][4];9 Ten intMain () One { Adp[0][1]=dp[0][2]=dp[0][3]=0; -dp[1][1]=dp[1][3]=1; -dp[1][2]=0; the - for(intI=2; i<= $; i++) - { -dp[i][1]=dp[i-1][3]+dp[i-2][3]; +dp[i][2]=dp[i-1][1]+dp[i-1][3]; -dp[i][3]=dp[i-1][1]+dp[i-2][1]; + } A at intN; - - while(cin>>N) - { - Long Longsum=dp[n][1]+dp[n][3]; - incout<<sum<<Endl; - } to return 0; +}View Code
Timus 1225 flags basic DP simple recursion