[蓝桥杯 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;
}
相关推荐
Benmao⁢10 小时前
C语言期末复习笔记
c语言·开发语言·笔记·leetcode·面试·蓝桥杯
leoufung12 小时前
LeetCode 22:Generate Parentheses 题解(DFS / 回溯)
算法·leetcode·深度优先
Bear on Toilet19 小时前
17 . 爬楼梯
算法·深度优先
leoufung1 天前
LeetCode 39. Combination Sum 题解(回溯 / DFS)
算法·leetcode·深度优先
roman_日积跬步-终至千里2 天前
【计算机算法与设计(6)】BFS、DFS、拓扑排序、连通分支
算法·深度优先·宽度优先
_OP_CHEN2 天前
【算法基础篇】(二十九)路径类线性 DP 保姆级教程:从矩阵到迷宫,覆盖 4 道经典题 + 优化神技
算法·矩阵·蓝桥杯·动态规划·算法竞赛·acm/icpc·路径类动态规划
leoufung2 天前
组合问题:为什么用start避免重复
算法·深度优先·图论
释怀°Believe3 天前
Daily算法刷题【面试经典150题-5️⃣图】
算法·面试·深度优先
_OP_CHEN3 天前
【算法基础篇】(二十八)线性动态规划之基础 DP 超详解:从入门到实战,覆盖 4 道经典例题 + 优化技巧
算法·蓝桥杯·动态规划·运筹学·算法竞赛·acm/icpc·线性动态规划
云泽8083 天前
蓝桥杯算法精讲:前缀和与差分算法的应用与实战
算法·职场和发展·蓝桥杯