*【每日一题 基础题】 [蓝桥杯 2023 省 B] 飞机降落

题目描述

N 架飞机准备降落到某个只有一条跑道的机场。其中第 i 架飞机在 Ti 时刻到达机场上空,到达时它的剩余油料还可以继续盘旋 Di 个单位时间,即它最早可以于 Ti 时刻开始降落,最晚可以于 Ti + Di 时刻开始降落。降落过程需要 Li个单位时间。

一架飞机降落完毕时,另一架飞机可以立即在同一时刻开始降落,但是不能在前一架飞机完成降落前开始降落。

请你判断 N 架飞机是否可以全部安全降落。
输入格式

输入包含多组数据。

第一行包含一个整数 T,代表测试数据的组数。

对于每组数据,第一行包含一个整数 N。

以下 N 行,每行包含三个整数:Ti,Di 和 Li。
输出格式

对于每组数据,输出 YES 或者 NO,代表是否可以全部安全降落。
样例输入

2

3

0 100 10

10 10 10

0 2 20

3

0 10 20

10 10 20

20 10 20
样例输出

YES

NO
提示

对于第一组数据,可以安排第 3 架飞机于 0 时刻开始降落,20 时刻完成降落。安排第 2 架飞机于 20 时刻开始降落,30 时刻完成降落。安排第 1 架飞机于 30 时刻开始降落,40 时刻完成降落。

对于第二组数据,无论如何安排,都会有飞机不能及时降落。

对于 30% 的数据,N ≤ 2。

对于 100% 的数据,1 ≤ T ≤ 10,1 ≤ N ≤ 10,0 ≤ Ti , Di , Li ≤ 105。

java 复制代码
import java.util.*;  

public class Main {  
    static final int N = 10;  
    static boolean[] st = new boolean[N];  
    static int n;  
    static boolean flag = false;  
    static int[] t = new int[N];  
    static int[] d = new int[N];  
    static int[] l = new int[N];  

    static void dfs(int u, int last) {  
        if (flag) return; // If we found one valid sequence, return  
        if (u == n) { // All planes have landed  
            flag = true;  
            return;  
        }  

        for (int i = 0; i < n; i++) {  
            if (!st[i]) { // If the current plane hasn't landed yet  
                if (t[i] + d[i] >= last) { // Check if it can wait for the last plane to land  
                    st[i] = true; // Mark the plane as landed  

                    if (t[i] > last) { // If this plane arrives after the last one has landed  
                        dfs(u + 1, t[i] + l[i]); // Update last to arrival + landing time  
                    } else { // If this plane arrives before or when the last one is landing  
                        dfs(u + 1, last + l[i]); // Wait for the last plane to land  
                    }  

                    st[i] = false; // Backtrack  
                } else {  
                    return; // If it can't wait, return  
                }  
            }  
        }  
    }  

    public static void main(String[] args) {  
        Scanner scanner = new Scanner(System.in);  
        int T = scanner.nextInt();  
        while (T-- > 0) {  
            n = scanner.nextInt();  
            for (int i = 0; i < n; i++) {  
                t[i] = scanner.nextInt();  
                d[i] = scanner.nextInt();  
                l[i] = scanner.nextInt();  
            }  

            for (int i = 0; i < N; i++) {  
                st[i] = false; // Reset the landing status  
            }  

            flag = false; // Reset the flag  
            dfs(0, 0); // Start DFS  

            if (flag) {  
                System.out.println("YES");  
            } else {  
                System.out.println("NO");  
            }  
        }  
    }  
}
java 复制代码
import java.util.*;
 
public class Main {
    static boolean flag=false;
    static boolean[] st;
    static int n;
 
    static Map<Integer,int[]> map;//
    static Scanner cin = new Scanner(System.in);
    public static void main(String[] args){
        int t= cin.nextInt();
        while(t--!=0)solve();
    }
 
    private static void solve() {
        n= cin.nextInt();
        flag=false;//标记,如果为true则搜素到答案
        st=new boolean[n];//初始胡标记数组,标记数组用于查询未安排下降的飞机,如果标记数组全为true,则代表能成功全部安排
        map=new HashMap<>();
        for (int i = 0; i < n; i++) {//输入
            int t= cin.nextInt();
            int d= cin.nextInt();
            int l= cin.nextInt();
            map.put(i,new int[]{t,d,l});
        }
        for (int i=0;i<n;i++){//枚举第一架飞机安排
            st[i]=true;//标记搜索
            dfs(map.get(i)[0]+map.get(i)[2]);//搜索下一驾飞机应该安排什么,并把时间传入下一个dfs,时间为飞机起飞时间加下降时间
            st[i]=false;//还原,取消标记
        }
        if(flag) System.out.println("YES");
        if(!flag) System.out.println("NO");
    }
 
    private static void dfs(int time) {
        boolean ok=true;//下面for循环中,如果没有再安排飞机,则代表已经成功安排所有飞机
        for (int i=0;i<n;i++){//枚举飞机
            if(st[i])continue;//如果已经下降了,不用再安排下降
            ok=false;
            if(time>map.get(i)[0]+map.get(i)[1])continue;
            int is=Math.max(time,map.get(i)[0])+map.get(i)[2];//时间取当前时间和飞机起飞时间最大那个,加上飞机下降时间
            st[i]=true;//标记
            dfs(is);//搜索下一驾飞机
            st[i]=false;//还原标记
        }
        if(ok)//代表搜素到答案 直接返回
            flag=true;
            return;
    }
}
相关推荐
水上冰石7 分钟前
comfui的sd1.5模型,有多少采样算法,详解每一个采样算法
人工智能·算法
黎阳之光24 分钟前
视频孪生+空天地水工融合,黎阳之光构建智慧水利监测新范式
大数据·人工智能·物联网·算法·安全
cheems952736 分钟前
[算法手记] 贪心 爬楼梯问题
算法·贪心算法
KaMeidebaby1 小时前
卡梅德生物技术快报|酵母双杂交 cDNA 文库构建与蛋白互作筛选流程
服务器·前端·数据库·人工智能·算法
sheeta19981 小时前
LeetCode 每日一题笔记 日期:2026.05.27 题目:3121. 统计特殊字母的数量 II
笔记·算法·leetcode
ST——Jess1 小时前
年度行业趋势研究报告:泛心理数字化赛道“流日推演”的算法困境与高保真交互范式重构
人工智能·算法·架构
Tisfy1 小时前
LeetCode 3300.替换为数位和以后的最小元素:一次遍历
数学·算法·leetcode·模拟
garmin Chen1 小时前
LeetcodeHot100打卡(14、合并空间,15、轮转数组,16、除了自身以外数组乘积,17.缺失的第一个整数)
java·笔记·学习·算法
elseif1232 小时前
【C++】vector 详细版
开发语言·c++·算法