第十四届蓝桥杯省赛C/C++大学B组真题-飞机降落


思路:根据数据范围N<=10猜测用DFS+剪枝,因为菜狗不会状压dp。根据题目,一般这种飞机的题都会用到贪心的思想。思想是每架飞机都要卡极限最早降落时间,从而保证后面的飞机能够有充足时间降落。
代码参考博客@MQy大佬有详细解答

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int n;
struct Plane {
    int t, d, l;
}p[N];

bool vis[N];
bool dfs(int pos, int last){
    if(pos == n) return true;
    for(int i = 0; i < n; ++i){
        int t = p[i].t, d = p[i].d, l = p[i].l;
        if(!vis[i] && t + d >= last){
            vis[i] = true;
            if(dfs(pos + 1, max(last, t) + l)) return true;
            vis[i] = false;
        }
    }
    return false;
}

int main(void){
    int T;
    cin >> 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(vis, 0, sizeof vis);
        if(dfs(0,0)) puts("YES");
        else puts("NO");
    }

    return 0;
}
相关推荐
dong_junshuai3 分钟前
每天一个开源项目#87 fmt:24.5K Stars 的 C++ 格式化核心
c++·开源·github
Navigator_Z13 分钟前
LeetCode //C++ - 1226. The Dining Philosophers
c语言·算法·leetcode
雪的季节1 小时前
Multisim14.0的详细中文安装步骤【附安装包】
c++
Persistent的粽子!2 小时前
C++:类与对象(二)
开发语言·c++
思麟呀2 小时前
嵌入式入门(一)宏观生态到首次烧录
c语言·嵌入式硬件
小雪崩2 小时前
嵌入式学习 day39:TCP并发服务器模型
linux·服务器·c语言·网络·学习·tcp/ip
蒸蒸yyyyzwd2 小时前
cpp 选手备战秋招学习笔记 day23
c++·求职招聘
.YM.Z2 小时前
C++ STL 容器深度剖析:vector 与 list 的模拟实现及对比
开发语言·c++·vector·list
无损检测小白白3 小时前
【嵌入式面试题一】
c语言·单片机
mengzhi啊4 小时前
QEventLoop loop配合loop.exec();替代while(1)。电脑cpu占用为0,使用while(1)cpu占用率100%
c++·qt