Button Bashing

转载原地址:https://blog.csdn.net/stdio_xuege/article/details/81001766

题目:

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 00seconds, and at most 11 hour. If a button press would result in a cooking time of less than 00seconds, the microwave will set the cooking time to 00 seconds. If a button press would result in a cooking time of more than 11 hour, the microwave will set the cooking time to 11hour. Initially, the microwave will run for 00seconds. There will always be a button adding at least 11 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 Format

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

  • one line with two space-separated integers nn and tt (1 \le n \le 16, 0 \le t \le 3600)(1≤n≤16,0≤t≤3600): the number of buttons available to change the cooking time, and the desired cooking time in seconds, respectively.
  • one line with nn space-separated integers b_ibi​ (-3600 \le b_i \le 3600)(−3600≤bi​≤3600): the number of seconds added to the cooking time when button ii is pressed.

Output Format

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.

样例输入

2
3 50
-10 10 60
1 50
20

样例输出

2 0
3 10

题意:

给出微波炉需要的定时,以及n个不同的按钮,不同的按钮对应不同的效果,增加或者减少一些时间,当然微波炉最多运行一个小时;

分析:要注意的点就是当时间<0或者>3600时,一定要写成=0或者=3600;

第二点,maze数组定义时一定要定义为一个非常大的数,其中maze[i]代表的是在时间i时使用了maze[i]个按钮

,    if(maze[no]<=maze[now]+1)
                    continue;

      如果在no时使用的按钮比now使用的按钮+1还要少,不能通过另外一个路径到时间no的按钮使用个数更少

就不必进行代换。

分析见代码;

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
#include<queue>
#include<algorithm>
using namespace std;
int a[10010],maze[10010];
int main()
{
	int i,n,m,f;
	queue<int> q;
	scanf("%d",&f);
	while(f--){
		scanf("%d %d",&n,&m);
		for(i=0;i<n;i++)
			scanf("%d",&a[i]);
		
		memset(maze,inf,sizeof(maze));
		maze[0]=0;
		q.push(0);
		
		while(!q.empty()){
			int now=q.front();
			q.pop();
			for(int j=0;j<n;j++){
				int no=now+a[j];
				if(no<0)
					no=0;
				if(no>3600)
					no=3600;
				if(maze[no]<=maze[now]+1)
					continue;
				q.push(no);
				maze[no]=maze[now]+1;
			}
		}
		
		int t;
		for( t=m;t<=3600;t++){
			if(maze[t]!=inf)
				break;
		}
		
		printf("%d %d\n",maze[t],t-m);
	}
	
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/dong_qian/article/details/81226946