[其它交流] 第十四届蓝桥杯:DFS之飞机降落

401 0
Honkers 2025-3-3 16:34:55 | 显示全部楼层 |阅读模式

这道题,由于它的数据范围是非常小的,我们可以采取暴力搜索的措施,把每种情况都枚举出来,如果有能行的情况就返回true

同时我们也要学会剪枝,如果已经确认飞机不能降落,就不要往下再展开了

  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. const int N = 30;
  5. using namespace std;
  6. int st[N];
  7. int t[N],d[N],l[N];
  8. int n,T;
  9. bool dfs(int pos,int end)
  10. {
  11. if(pos>n)
  12. {
  13. return true;
  14. }
  15. for(int i = 1;i<=n;i++)
  16. {
  17. if(st[i]) continue;
  18. if(end > t[i]+d[i]) continue;
  19. int newend = max(end,t[i])+l[i];
  20. st[i] = true;
  21. if(dfs(pos+1,newend)) return true;
  22. st[i] = false;
  23. }
  24. return false;
  25. }
  26. int main()
  27. {
  28. cin >> T;
  29. while(T--)
  30. {
  31. memset(st,0,sizeof(st));
  32. cin >> n;
  33. for(int i = 1;i<=n;i++)
  34. {
  35. cin >> t[i] >> d[i] >> l[i];
  36. }
  37. if(dfs(1,0)) cout << "YES" << endl;
  38. else cout << "NO" << endl;
  39. }
  40. return 0;
  41. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

特级红客

关注
  • 3281
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行