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
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?
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)
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.