Timus 1225 flags basic DP simple recursion

Source: Internet
Author: User

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:
    1. Stripes of the same color cannot is placed next to all other.
    2. 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
input Output
3
4

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.