拿到这题,考虑了一下写记忆化搜索,dfs(int id,int bottom,int k),id表示当前考虑的积木,bottom表示该积木下的积木序号,k表示当前考虑第id块积木的第k面。我们用f[i][j][k]表示当前在搭第i根柱子,考虑第j个积木的第k面。于是,每个积木都有3个决策,要么放在上一个积木的上面(有条件约束),要么去搭下一根柱子,要么不用它。所以,我们很容易就可以写出状态转移方程。
AC code:
#include <stdio.h>
#include <string.h>
#include <iostream>
#define Re register int
using namespace std;
int N,M,x1,y1,x2,y2,d[105][3],f[105][105][3],ans;
inline int read()
{
int x=0,w=0; char ch=0;
while (!isdigit(ch)) w|=ch=='-',ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return w?-x:x;
}
inline int max(int a,int b) {return a>b?a:b;}
int main(int argc, char const *argv[])
{
N=read(); M=read();
for (Re i=1; i<=N; ++i)
d[i][0]=read(),d[i][1]=read(),d[i][2]=read();
memset(f,0x80,sizeof(f));
f[0][0][0]=f[0][0][1]=f[0][0][2]=0;
for (Re i=1; i<=M; ++i)
for (Re j=1; j<=N; ++j) //枚举要做决策的积木
for (Re b=0; b<j; ++b) //枚举底部的积木,0表示没有(新搭一个柱子)
for (Re bs=0; bs<3; ++bs) //枚举底部积木的面
for (Re ns=0; ns<3; ++ns) //当前积木的面
{
x1=d[b][bs]; y1=d[b][(bs+1)%3];
x2=d[j][ns]; y2=d[j][(ns+1)%3];
if (x1>y1) swap(x1,y1);
if (x2>y2) swap(x2,y2);
if (x1>=x2&&y1>=y2)
f[i][j][ns]=max(f[i][j][ns],f[i][b][bs]+d[j][(ns+2)%3]);
f[i][j][ns]=max(f[i][j][ns],f[i-1][b][bs]+d[j][(ns+2)%3]);
}
for (Re i=1; i<=N; ++i)
ans=max(ans,max(f[M][i][0],max(f[M][i][1],f[M][i][2])));
printf("%d\n",ans);
return 0;
}QWQ这是本蒟蒻的第一篇题解,点个赞再走吧QWQ