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