HDU 2045 LELE's RPG puzzle (recursive) or (moving return)

Not easy series (3) - LELE's RPG puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 62635    Accepted Submission(s): 24959


Problem Description
The super idol LELE, known as the "AC Girl Killer", has suddenly played a deep game recently, which has upset many "Cole" (LELE's fans, namely "Coke"). After many investigations, a senior Cole finally knew the reason. It turns out that LELE has recently studied the famous RPG problem:

there are n squares in a row, and each square is painted with three colors of red (Red), pink (Pink), and green (Green). No adjacent squares can be of the same color, and the first and last squares cannot be of the same color. Find all the painting methods that meet the requirements. The

above is the famous RPG problem.

If you are Cole, I think you will try your best to help LELE solve this problem; if not, see the face of many beautiful Cole women who are dying Come on, you won't stand idly by, will you?

 

Input
The input data contains multiple test instances, each test instance occupies a row, which consists of an integer N, (0<n<=50).
 

Output
For each test instance, please output all the coating methods that meet the requirements, and the output of each instance occupies one line.
 

Sample Input
 
  
12
 
Sample Output
 
  
36
 


Definition f(n) := the number of filling schemes for n grids


f(1) = 3 R G B f(2) = 6 RG RB GR GB BR BG f(3) = 6 RGB RBG GRB GBR BRG BGR f(4 ) = 18 RGRG RGRB RGBG RBRB RBRG RBGB GRGR GRGB GRBR GBGB GBGR GBRB BRBR BRBG BRGR BGBG BGBR BGRG There are a total of n grids  in recursive thinking . When filling the nth grid with color, you should pay attention:     the first n-1 grids are filled with f(n- 1) There are filling schemes, so there is one scheme for the nth cell (because the n-1th cell must be a different color from the first cell) 

































 
    There are f(n-2) filling schemes when filling the first n-2 cells, so the last two cells have 2 schemes (because the n-1th cell must be the same color as the first cell),  so: f(n) = f( n-1) + 2*f(n-2) n>3
 

 

In general, if there are n lattices, there are f(n) solutions

Let's first discuss the case of the first n-1 lattices:

        If the first n-1 grids are filled, that is, the color of the first grid is different from that of the n-1 grid, then the n grid can only be filled with one color, which is the same color as the n-1 and the first grid. The color of the grid is different. At this time, there are a total of f(n-1) cases.

        If the first n-1 cells are not filled, the first n-2 cells are filled. Then the last two grids can only be filled with two colors, so there are 2*f(n-2) ways to put them


The final recursive formula is f(n)=f(n-1)+2*f(n-2) 



#include<cstdio>
#define ll long long
ll a[62];
void init()
{
	a[0]=3;
	a[1]=3;
	a[2]=6;
	a[3]=6;
	for(int i=4;i<62;i++)
		a[i]=a[i-1]+2*a[i-2];
 }
intmain()
{
	int n;
	init();
	while(~scanf("%d",&n))
		printf("%lld\n",a[n]);
	return 0;
}




My strength is limited, please feel free to point out any mistakes, thank you.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325743320&siteId=291194637