[蓝桥杯 2023 省 B] 飞机降落(暴搜DFS+贪心)

总结:为什么你看到题想不出来怎么写呢,我想不到这道题还会用到dfs的思想,顶多能知道可能会有贪心,还是得多做题。

这道题让我想起来导弹拦截借教室,记得有空做做!!不要研究难题,把基本算法研究透了!!!别死磕,要灵活!

DFS代码:

cpp 复制代码
//要用DFS 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;

int tt;
int n;
struct node{
	int t;
	int d;
	int l;
};
node a[15];

bool st[15];

bool dfs(int u,int last){
	if(u == n) return true;//如果搜索过所有点
	for(int i=0;i<n;i++){
		int t=a[i].t,d=a[i].d,l=a[i].l;
		if(!st[i] && t+d >= last)//如果没有被搜索过,并且 这一次a[i]的开头在上一次结束的时间后面
		{
			st[i] = true;//能被搜索
			if(dfs(u+1,max(last,t)+l))
				return true;
			st[i] = false;//恢复现场	
		} 
	} 
	return false; 
}
int main()
{
	scanf("%d",&tt);
	while(tt--){

		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d%d%d",&a[i].t,&a[i].d,&a[i].l);
			//或者scanf("%d%d%d",&t,&d,&l);
			//    p[i] = {t,d,l}; 
		}
		memset(st,false,sizeof(st));
		if(dfs(0,0)) puts("YES");
		else puts("NO");
	}
	return 0;
}
相关推荐
The Chosen One9851 小时前
分享对dp题目的理解-不断更新ing
笔记·算法·深度优先·动态规划·dp
浅念-10 小时前
递归解题指南:LeetCode经典题全解析
数据结构·算法·leetcode·职场和发展·排序算法·深度优先·递归
2301_800895101 天前
第十四届蓝桥杯国赛b组真题---备战国赛版h
算法·蓝桥杯·深度优先
WL_Aurora1 天前
备战蓝桥杯国赛【Day 12】
python·蓝桥杯
Mr_pyx1 天前
LeetCode 226. 翻转二叉树(多种解法详解)
算法·深度优先
如竟没有火炬2 天前
用队列实现栈
开发语言·数据结构·python·算法·leetcode·深度优先
每天回答3个问题2 天前
LeetCodeHot100|回溯算法、46.全排列、78.子集、17.电话号码的字母组合
算法·深度优先·回溯
洛水水2 天前
【力扣100题】38.路径总和 III
算法·leetcode·深度优先
宵时待雨2 天前
回溯算法专题1:递归
数据结构·c++·笔记·算法·leetcode·深度优先
洛水水2 天前
【力扣100题】40.二叉树中的最大路径和
算法·leetcode·深度优先