DFS做法
- 搜索全排列所有顺序
- 找到顺序后看此顺序是否可以成功降落
#include<bits/stdc++.h>
using namespace std;
#define int long long
// #define max(a,b) ((a)>(b))?a:b
// #define min(a,b) ((a)<(b))?a:b
#define pb push_back
// check 结果是否满足
bool check(int n,vector<int>&ans, vector<int>&T, vector<int>&D, vector<int>&L) {
int curTime = 0;
for (int i=1; i<=n; i++) {
// 当前时间未到起飞时间
if (curTime < T[ans[i]]) curTime = T[ans[i]]+L[ans[i]];
// 当前时间在起飞时间
else if (curTime >= T[ans[i]] && curTime <= T[ans[i]]+D[ans[i]])
curTime += L[ans[i]];
else return false;
}
return true;
}
// 当前位置是第x个飞机, ans数组存飞机顺序
int flag = 0;
void dfs(int n, int x, vector<int>&ans, vector<int>&T, vector<int>&D, vector<int>&L, vector<bool>&vis) {
if (x == n+1) {
// for (int i=1; i<=n; i++) {
// cout << ans[i] << ' ';
// }
// cout << '\n';
if (check(n,ans,T,D,L))
flag = 1;
return;
}
for (int i=1; i<=n; i++) {
if (vis[i]) { //第i架飞机可选
ans[x] = i;
vis[i] = false;
dfs(n,x+1,ans,T,D,L,vis);
ans[x] = 0;
vis[i] = true;
}
}
}
void solve() {
int n; cin >> n;
vector<int> T(n+1), D(n+1), L(n+1), ans(n+1, 0);
vector<bool> vis(n+1, true);
flag = 0;
for (int i=1; i<=n; i++) cin >> T[i] >> D[i] >> L[i];
// for (int i=1; i<=n; i++) {
// cout << T[i] << ' ' << D[i] << ' ' << L[i] << '\n';
// }
dfs(n,1,ans,T,D,L,vis);
if (flag) cout << "YES\n";
else cout << "NO\n";
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t; cin >> t;
while (t--){
solve();
}
return 0;
}