zoukankan      html  css  js  c++  java
  • 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

    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
  • 相关阅读:
    挂断电话——黑名单拦截
    上传文件 服务端模拟存储
    短信监听+短信拦截
    c#常用控件缩写(装)
    20121016学习笔记四
    c#日期时间格式化
    FTP服务器配置以及访问
    关于远程桌面设置和连接
    20121016学习笔记三
    电脑开机应用程序自动启动设置
  • 原文地址:https://www.cnblogs.com/-maybe/p/4461626.html
Copyright © 2011-2022 走看看