码迷,mamicode.com
首页 > 其他好文 > 详细

timus 1225 flags 基础DP 简单递推

时间:2015-04-27 23:41:04      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

 

                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:
技术分享

Input

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

Output

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

Sample

inputoutput
3
4

 

 

 

 

 

 

1代表白色

2.蓝色

3.红色

 

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

 

然后递推

 

注意:

1.要用 long long

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

 

技术分享
 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

 

 

 

 

 

 

 

 

 

 

 

 

timus 1225 flags 基础DP 简单递推

标签:

原文地址:http://www.cnblogs.com/-maybe/p/4461626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!