Button Bashing(bfs)

                                                     Button Bashing

                                                                        时间限制: 1 Sec  内存限制: 128 MB
                                                                                    提交: 47  解决: 14
                                                                        [提交][状态][讨论版][命题人:admin]

escription

You recently acquired a new microwave, and noticed that it provides a large number of buttons to be able to quickly specify the time that the microwave should be running for. There are buttons both for adding time, and for subtracting time. You wonder how efficient you can be when entering cooking times: you want to minimize the number of required button presses.

The microwave can be running for at least 0 seconds, and at most 1 hour. If a button press would result in a cooking time of less than 0 seconds, the microwave will set the cooking time to 0 seconds. If a button press would result in a cooking time of more than 1 hour, the microwave will set the cooking time to 1 hour. Initially, the microwave will run for 0 seconds. There will always be a button adding at least 1 second to the cooking time.

Given the buttons that the microwave provides for entering cooking times, determine the least amount of button presses required to let the microwave run for a certain amount of time. If it is not possible to enter the desired cooking time precisely, determine the smallest achievable cooking time above the target, and the minimum number of button presses required for that cooking time, instead. The microwave does not allow to adjust the cooking time once it has started cooking.

Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with two space-separated integers n and t (1 ≤ n ≤ 16 and 0 ≤ t ≤ 3600): the number of buttons available to change the cooking time, and the desired cooking time in seconds, respectively.

  • one line with n space-separated integers bi (-3600 ≤ bi ≤ 3600): the number of seconds added to the cooking time when button i is pressed.

Output

Per test case:

one line with two space-separated integers: the minimum number of button presses required to reach the required cooking time, and the minimum number of extra seconds that the microwave must be running for, respectively.

Sample Input

2
3 50
-10 10 60
1 50
20

Sample Output

2 0
3 10

此题为bfs,首先先找出按一次能得到的值并记录按的按钮数,紧接着按第二次能得到的值,

并判断此时的值在之前有没有出现过,若有,不入队,因为之前出现的肯定按钮数更少

入队更新了。若之前没有出现过,那就入队,并在之前到达当前值item的步数加1。


#include<cstdio>

#include<algorithm>

#include<queue>

#include<cstring>

using namespace std;

#define INF 0x3f3f3f3f

int main()

{

    int rode[4000];

 

    int n,m,num[20],Case;

    scanf("%d",&Case);

    while(Case--)

    {

         queue<int> que;

        scanf("%d%d",&n,&m);

        for(int i=1;i<=n;i++)

            scanf("%d",&num[i]);

        memset(rode,INF,sizeof(rode));

        que.push(0);

        rode[0]=0;

        while(!que.empty())

        {

            int item=que.front();

            que.pop();

            for(int i=1;i<=n;i++)///此bfs处,

            {

                int result=item+num[i];

                if(result<0) result=0;

                else if(result>3600) result=3600;

                if(rode[result]<=rode[item]+1)

                    continue;///说明值result在更早的时候已经得到了,所以不需入队,不需更新步数

                que.push(result);

                rode[result]=rode[item]+1;///说明值result在此之前还没有得到过,此事值result的步数就为当前到达值item

                                        ///的步数加1

            }

        }

        int t;

        for( t=m;t<=3600;t++)

            if(rode[t]!=INF){

        break;}

        printf("%d %d\n",rode[t],t-m);

    }

    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值