timus 1225 flags 基础DP 简单递推

 

                1225. Flags

Time limit: 1.0 second
Memory limit: 64 MB
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 output
3
4

 

 

 

 

 

 

1代表白色

2.蓝色

3.红色

 

dp[1][j]表示选到第i个条纹,第i个为j这种颜色的

 

然后递推

 

注意:

1.要用 long long

2.蓝色不能放在最后面和最前面,只能在中间

 

timus 1225 flags 基础DP 简单递推
 1 #include<iostream>

 2 #include<cstring>

 3 

 4 using namespace std;

 5 

 6 const int maxn=48;

 7 

 8 long long dp[maxn][4];

 9 

10 int main()

11 {

12     dp[0][1]=dp[0][2]=dp[0][3]=0;

13     dp[1][1]=dp[1][3]=1;

14     dp[1][2]=0;

15 

16     for(int i=2;i<=45;i++)

17     {

18         dp[i][1]=dp[i-1][3]+dp[i-2][3];

19         dp[i][2]=dp[i-1][1]+dp[i-1][3];

20         dp[i][3]=dp[i-1][1]+dp[i-2][1];

21     }

22 

23     int n;

24 

25     while(cin>>n)

26     {

27         long long  sum=dp[n][1]+dp[n][3];

28 

29         cout<<sum<<endl;

30     }

31     return 0;

32 }
View Code

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(flag)