第十四届蓝桥杯省赛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;
}
相关推荐
小师兄吃牛肉43 分钟前
C语言篇:内联函数
c语言·开发语言
白山编程大哥1 小时前
C语言篇:语法进阶
c语言·数据结构·算法
jimy11 小时前
虚函数vtable,链接时看到构造函数,linker把符号地址写入vtable(三)
开发语言·c++
Logic1012 小时前
C语言/数据结构动态规划题解:爬楼梯(含坏台阶)——避开障碍物的路径计数
c语言·数据结构·动态规划·时间复杂度·空间复杂度·算法题·爬楼梯
山下梅子酒2252 小时前
洛谷-入门-B2081
c语言
潼心1412o2 小时前
C++初阶(长期更新)第9讲:string(上)
开发语言·c++
朝朝辞暮i3 小时前
C++ 第 39 章: 阶段性总复习——类、对象、构造、继承、this、指针与智能指针
开发语言·c++·算法·ros2
wuminyu3 小时前
Virtual Thread重投递至ForkJoinPool任务队列过程解析
java·linux·c语言·jvm·c++
All for pursuit.3 小时前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
Navigator_Z3 小时前
LeetCode //C - 1281. Subtract the Product and Sum of Digits of an Integer
c语言·算法·leetcode