Not easy series of (3)--lele RPG puzzles

Source: Internet
Author: User

There are rows of n squares, with red (red), powder (Pink), Green (green) Three colors each lattice, each color, requires that any adjacent squares can not be the same color, and the first and the first two squares also different colors. All the requirements of the coating method.

Idea: Using recursive algorithms.

A[1]=3,a[2]=6,a[3]=6

There are two kinds of situations to discuss:

The first type: (n-1) lattice and the first lattice of different colors, then Tu (n-1) lattice a total of a[n-1] method, because (n-1) lattice and the first lattice of different colors, the last lattice only one color can be coated.

The second type: (n-1) lattice and the first lattice of the same color, the front of the (n-2) lattice total a[n-2] species of color, plus the (n-1) lattice because with the first lattice color, so it will not be the same as (N-2) a lattice, at this time the last lattice has two kinds of tinted method, All common 2*a[n-2] methods.

Sum: a[n]=a[n-1]+2*a[n-2]

1#include <iostream>2 using namespacestd;3 intMain ()4 {5     Long Longa[ -];6a[1] =3;7a[2] =6;8a[3] =6;9     intN;Ten      while(cin>>N) One     { A          for(intI=4; I <= N; i++) -         { -A[i] = a[i-1] +2* A[i-2]; the         } -cout << A[n] <<Endl; -     } -}

Not easy series of (3)--lele RPG puzzles

Related Article

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.