dfs 枚举飞机降落的顺序(枚举所有的情况)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 15;
int n;
struct Plane{
int t,d,l;
}p[N];
bool st[N];
bool dfs(int u,int last){
if(u==n) return true;
for(int i=0;i<n;i++){
int t=p[i].t,d=p[i].d,l=p[i].l;
if(!st[i]&&t+d>=last){
st[i]=true;
if(dfs(u+1,max(last,t)+l)) return true;
st[i]=false;
}
}
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d", &n);
for(int i=0;i<n;i++){
int t,d,l;
scanf("%d%d%d",&t,&d,&l);
p[i]={t,d,l};
}
memset(st,false,sizeof st);
if(dfs(0,0)) printf("YES\n");
else printf("NO\n");
}
return 0;
}
状态压缩dp
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10,M=1<<N,INF=0x3f3f3f3f;
int T,n;
int f[M];
struct Plane{
int t,d,l;
}p[N];
int main()
{
scanf("%d", &T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++){
int t,d,l;
scanf("%d%d%d",&t,&d,&l);
p[i]={t,d,l};
}
memset(f,0x3f,sizeof f);
f[0]=0;
for(int i=1;i<1<<n;i++){
for(int j=0;j<n;j++){
int t=p[j].t,d=p[j].d,l=p[j].l;
if((i>>j)&1){
int last=f[i-(1<<j)];
if(t+d>=last){
f[i]=min(f[i],max(last,t)+l);
}
}
}
}
if(f[(1<<n)-1]!=INF) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}