套用全排列做法(acwing,过不了全部数据,蓝桥,C语言网可以AC)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 15;
int t[N] , d[N] , l[N];
int n;
bool flag_now = false;
bool flag = false;
bool st[N];
int path[N];
void dfs(int u)
{
if(flag)return;
if(u > n)
{
// for(int i = 1;i <= n;i++)
// printf("%d ",path[i]);
// printf("\n");
flag_now = true;
int last = -1;
for(int i = 1;i <= n;i++)
{
if(i == 1)
{
last = t[path[1]] + l[path[1]];
}
else
{
if(t[path[i]] + d[path[i]] < last)
{
flag_now = false;
break;
}
else
{
if(t[path[i]] > last)last = t[path[i]] + l[path[i]];
else last = last + l[path[i]];
}
}
}
if(flag_now)
{
flag = true;
return;
}
}
for(int i = 1;i <= n;i++)
{
if(!st[i])
{
st[i] = true;
path[u] = i;
dfs(u + 1);
path[u] = 0;
st[i] = false;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d", &n);
for(int i = 1;i <= n;i++)
scanf("%d%d%d",&t[i],&d[i],&l[i]);
flag = false;
memset(st,0,sizeof st);
dfs(1);
if(flag)printf("YES\n");
else printf("NO\n");
}
}