• 飞机降落(dfs全排列)


    4957. 飞机降落 - AcWing题库

    数据量很小,直接爆搜

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int N=20;
    4. int n,t,flag,st[N];//st记录是否已经降落,flag标记是否降落完成
    5. struct Node
    6. {
    7. int t,d,l;
    8. }node[N];
    9. void dfs(int u,int last)//u表示已经降落的飞机数量,last表示上一架飞机降落完成的时间
    10. {
    11. if(u>=n)//已经全部降落
    12. {
    13. printf("YES\n");
    14. flag=1;
    15. return;
    16. }
    17. if(flag)return;//已经完成就不再搜索
    18. // printf("%d %d\n",u,last);
    19. for(int i=0;i<n;i++)
    20. {
    21. if(st[i]==0)
    22. {
    23. int t=node[i].t,d=node[i].d,l=node[i].l;
    24. if(t+d<last)return;//如果最迟降落时间已经过去就不满足,剪掉
    25. st[i]=1;
    26. dfs(u+1,max(last,t)+l);
    27. st[i]=0;
    28. }
    29. }
    30. }
    31. int main()
    32. {
    33. scanf("%d",&t);
    34. while(t--)
    35. {
    36. memset(st,0,sizeof st);
    37. flag=0;
    38. scanf("%d",&n);
    39. for(int i=0;i<n;i++)
    40. {
    41. int t,d,l;
    42. scanf("%d %d %d",&t,&d,&l);
    43. node[i]={t,d,l};
    44. }
    45. dfs(0,0);
    46. if(flag==0)printf("NO\n");
    47. }
    48. return 0;
    49. }

    next_permutation:

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. int n , m , k ;
    4. struct{
    5. int t , d , l ;
    6. }g[15] ;
    7. int a[15] ;
    8. void solve()
    9. {
    10. ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
    11. cin >> n ;
    12. for(int i = 1 ; i <= n ; i ++) a[i] = i;
    13. for(int i = 1 ; i <= n ; i ++) cin >> g[i].t >> g[i].d >> g[i].l ;
    14. int f = 0 ;
    15. do{
    16. int time = 0 ; // 上一架飞机的降落时间,在此事件后开始降落的飞机都是合法的
    17. for(int i = 1 ; i <= n ; i ++)
    18. {
    19. int j = a[i] ;
    20. if(time > g[j].t + g[j].d) break ; // 如果大于最晚到达时间则不合法
    21. time = max(time , g[j].t) ; // 和飞机到达时间取max,
    22. time += g[j].l ; // 当前飞机降落时间
    23. if(i == n) f = 1 ; // 所有飞机都能降落
    24. }
    25. if(f) break ;
    26. }while(next_permutation(a + 1 , a + 1 + n)) ; // n的全排列
    27. cout << (f?"YES\n":"NO\n") ;
    28. }
    29. int main()
    30. {
    31. int t ; cin >> t ;
    32. while(t --) solve() ;
    33. return 0;
    34. }

     

  • 相关阅读:
    typescript86-react中类组件的类型
    Monkey测试
    Shiro 550、721
    c语言练习48:总结字符函数和字符串函数
    Java Socket 网络编程
    【AcWing16】【LeetCode】并查集Union Find-128/130/*1020-学完广度优先/深度优先要回来再看
    react实现路由拦截器
    flink中使用GenericWriteAheadSink的优缺点
    NowCode JZ39 数组中出现次数超过一半的数字 简单
    uniapp 分包
  • 原文地址:https://blog.csdn.net/QZZ_PP/article/details/132741898