「日常训练」 不容易系列之(3)—— LELE的RPG难题 (HDU 2045)
题目简述
有排成一行的个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色,求全部的满足要求的涂法。
分析
首先感谢题解,我个人想半天是出不来的。
考虑第n个与第n-1个的关系。因为第n个不能和第1个同色,那么第n个颜色就是我们判断的突破口,而它是由第n-1个决定的。如果第n-1个保持原来的状况不变,那么第n个只能有一种可能,整个情况数就是原来的;如果第n-1个不保持原来的情况(那么就只会是一种颜色——和第一个一样),那么相当于n-2是最后一个。这样一来,最后一色有两种情况,这种大情况的整个数目就是。边界条件处理好即可。
这种递推题完成后,一定要代入最大值要看是否越界!!!
代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
#define pr(x) cout << #x << " = " << x << " ";
#define prl(x) cout << #x << " = " << x << endl;
#define ZERO(X) memset((X),0,sizeof(X))
#define ALL(X) (X).begin(),(X).end()
#define SZ(x) (int)x.size()
using namespace std;
typedef pair<int, int> PI;
typedef pair<pair<int, int>, int> PII;
typedef pair<pair<pair<int, int>, int>, int> PIII;
using ull= unsigned long long;
using ll = long long;
using ld = long double;
#define quickio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
/* debug("Precalc: %.3f\n", (double)(clock()) / CLOCKS_PER_SEC);
clock_t z = clock();
solve();
//debug("Test: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
*/
template<typename T = int>
inline T read() {
T val=0, sign=1;
char ch;
for (ch=getchar();ch<'0'||ch>'9';ch=getchar())
if (ch=='-') sign=-1;
for (;ch>='0'&&ch<='9';ch=getchar())
val=val*10+ch-'0';
return sign*val;
}
int main()
{
ll arr[55]={0,3,6,6};
for(int i=4;i<51;i++)
arr[i]=arr[i-1]+2*arr[i-2];
int n;
while(cin>>n)
{
cout<<arr[n]<<endl;
}
return 0;
}
如非注明,原创内容遵循GFDLv1.3发布;其中的代码遵循GPLv3发布。
分类:
题目记录
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术