• BAPC 2014:Button Bashing(暴力+bfs)


    题意:

    给出n,m,代表微波炉有n个按钮,要求达到总时间为m

    然后给出n个数,代表n个按钮能增加的时间,问最少几步,能够使得按出的总时间大于等于要求的时间,并且相差最小

    输出最小的步数与相差的最小值

    要求,当总时间小于0时,时间为0,大于3600时,时间为3600

    题意:

    给出n,m,代表微波炉有n个按钮,要求达到总时间为m

    然后给出n个数,代表n个按钮能增加的时间,问最少几步,能够使得按出的总时间大于等于要求的时间,并且相差最小

    输出最小的步数与相差的最小值

    要求,当总时间小于0时,时间为0,大于3600时,时间为3600

    Sample Input

    2
    3 50
    -10 10 60
    1 50
    20

    Sample Output

    2 0
    3 10

    AC代码:

    #include<stdio.h>
    #include<queue>
    #include<string.h>
    #define INF 0x3f3f3f3f
    using namespace std;
    int step[4000];
    int a[20];
    int main()
    {
        int t,n,m,v,nt,i;
        scanf("%d",&t);
        while(t--)
        {
            memset(step,INF,sizeof(step));
            queue<int>q;
            scanf("%d%d",&n,&m);
            for( i=0 ; i<n ;i++)
                scanf("%d",&a[i]);
            q.push(0);
            step[0]=0;///记录步数
            while(!q.empty())
            {
                 v=q.front();
                q.pop();
                for( i=0 ; i<n ;i++)
                {
                    nt=v+a[i];///时间点
                    if(nt<0)
                        nt=0;
                    if(nt>3600)
                        nt=3600;
                    if(step[nt]<=step[v]+1)
                        continue;
                     step[nt]=step[v]+1;
                     q.push(nt);
                }
            }
            for( i=m; i<=3600;i++)
                if(step[i]!=INF)
                break;
            printf("%d %d
    ",step[i],i-m);
        }
    }
    View Code

    做题报告:

    题意要理解清楚,要明白小于0为0,大于3600为3600,大胆暴力

  • 相关阅读:
    hdfs command
    开机启动
    date
    tabulate
    django前后端分离403 csrf token missing or incorrect
    设计一个程序,程序中有三个类,Triangle,Lader,Circle。
    总结,
    数据库2
    JDBC数据库1
    网络编程2
  • 原文地址:https://www.cnblogs.com/shuaihui520/p/8952248.html
Copyright © 2020-2023  润新知