第二次训练-B-Button Bashing(突破我对bfs的固有看法)

题意概括:1.输入T(表示数据数)

                2.输入电饭煲按钮(n)和煲饭需要时间(time)

                3.输入n个按钮调控的时间

        注意:输入时间单位为秒,且电饭煲不支持时间小于0或大于一小时。

样例输入
2
3 50
-10 10 60
1 50
20
样例输出
2 0
3 10

这里的代码用step[i]来记录第“step[i]”步调到了i这个时间点。

第一次发现数组还能用来这样记录

0x3f3f3f3f的用法也是第一次接触。

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int mp[20];
int step[4000];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,time;
		int i,v,tt;
		cin>>n>>time;
		for(int i=0;i<n;i++) cin>>mp[i];
		memset(step,0x3f3f3f3f,sizeof(step));
		queue<int> q;
		q.push(0);
		step[0]=0;
		while(!q.empty())
		{
			v=q.front();
			q.pop();
			for(int i=0;i<n;i++)
			{
				tt=v+mp[i];
				if(tt<0) tt=0;
				if(tt>3600) tt=3600;
				if(step[tt]<=step[v]+1) continue ;
				step[tt]=step[v]+1;
				q.push(tt); 
			}
		}
		
		for(i=time;i<=3600;i++)
		{
			if(step[i]!=0x3f3f3f3f) 
			{
				break;
			}
		}
		printf("%d %d\n",step[i],i-time);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值