HDU 2045 RPG难题

http://acm.hdu.edu.cn/showproblem.php?pid=2045

这道题也是用倒推:

先假设前n-2个块都已经涂好,涂第n-1块时有以下两种情况:

1.n-1和1相同,则n有两种涂法。退回n-2接着推

2.n-1和1不同,则n只有一种涂法,退回n-1接着推

 1 #include<iostream>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 
 7 int main()
 8 {
 9           int n;
10           while(cin>>n && n)
11           {
12                     long long a[51];
13                     a[1] = 3;
14                     a[2] = 6;
15                     a[3] = 6;
16                     for(int i = 4;i<=n;i++)
17                     {
18                               a[i] = a[i-1]+2*a[i-2];
19                     }
20                     cout<<a[n]<<endl;
21           }
22 
23 }
原文地址:https://www.cnblogs.com/qlky/p/4959022.html