目录

P9241 [蓝桥杯 2023 省 B] 飞机降落

题目描述

题解

这道题数据范围很小,所以可以用搜索做。具体题解看下方代码。

代码

带注释版代码

c 复制代码
#include<iostream>
#include<cstring>
using namespace std;

const int N=10;

struct Plane{ // 存每个飞机的t,d,l 
	int t;
	int d;
	int l;
}planes[N];

int n;
bool st[N]; // 用来判断该飞机有没有降落 

bool dfs(int u,int last){ // u代表当前降落飞机数,last代表当前的时间 
	if(u==n){ // 如果降落了n个飞机,说明所有飞机安全降落 
		return true;
	}
	// dfs有n条分支(当前可以选择n个飞机进行降落) 
	for(int i=0;i<n;i++){
		// 如果!st[i]代表飞机没有降落,
		// 并且飞机达到的时间和盘旋的时间大于当前的时间,就说明这个飞机是安全的(没有到达或者正在盘旋中) 
		if(!st[i]&&planes[i].t+planes[i].d>=last){
			st[i]=true; // 飞机降落
			// 下一个飞机进行降落,并且当前时间要进行更新。
			// last更新是,如果last大于飞机到达时间的话,则该飞机在last时刻降落,并耗时l时,last更新为last+l
			// 如果last小于飞机到达时间的话,那么需要等飞机到达后再降落,所有last更新为t+l时
			// 如果所有飞机都安全降落了,即u==n,return true,那么dfs回溯时,下方条件判断为true,也return true,终止后续递归 
			if(dfs(u+1,max(last,planes[i].t)+planes[i].l))return true;
			// 回溯要把修改复原 
			st[i]=false;
		}
	}
	// 如果当前找不到降落的飞机,返回false 
	return false;
}

int main(){
	int T;
	cin>>T;
	for(int i=0;i<T;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			int t,d,l;
			cin>>t>>d>>l;
			planes[j]={t,d,l};
		} 
		cout<<(dfs(0,0)?"YES":"NO")<<endl;
		// 因为上面的dfs满足u==n,会直接return true,并层层回溯返回true,
		//这个时候不会将修改复原(st[i]=false),所以要把可能存在的st[i]==true的情况消灭,重新初始化一下st数组 
		memset(st,0,sizeof st);
	}
}

不带注释版代码

c 复制代码
#include<iostream>
#include<cstring>
using namespace std;

const int N=10;

struct Plane{
	int t;
	int d;
	int l;
}planes[N];

int n;
bool st[N];

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

int main(){
	int T;
	cin>>T;
	for(int i=0;i<T;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			int t,d,l;
			cin>>t>>d>>l;
			planes[j]={t,d,l};
		} 
		cout<<(dfs(0,0)?"YES":"NO")<<endl;
		memset(st,0,sizeof st);
	}
}
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
UpUpUp……1 小时前
模拟String基本函数/深浅拷贝/柔性数组
开发语言·c++·算法
L_M_TY1 小时前
D. Bash and a Tough Math Puzzle
数学·算法·线段树·gcd
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧1 小时前
C语言_数据结构总结9:树的基础知识介绍
c语言·开发语言·数据结构·b树·算法·visualstudio·visual studio
Dann Hiroaki2 小时前
文献分享: 对ColBERT段落多向量的剪枝——基于学习的方法
学习·算法·剪枝
三三木木七2 小时前
神经网络的基本知识
人工智能·神经网络·算法
_extraordinary_2 小时前
穷举vs暴搜vs深搜vs回溯vs剪枝刷题 + 总结
算法·深度优先·剪枝
studyer_domi2 小时前
matlab 三维桥式起重机系统数学模型
人工智能·算法·matlab
曦月逸霜3 小时前
第十次CCF-CSP认证(含C++源码)
数据结构·c++·算法·ccf-csp
滨HI03 小时前
1314:【例3.6】过河卒(Noip2002)--DP>DFS【超时DFS】
算法·深度优先
Y编程小白3 小时前
JVM--垃圾回收
jvm·算法