Button Bashing(搜索)

题意:有 n 个按钮 ,每个按钮都可以增加或者减少时间,可以按多次,问如何按这些按钮使得与目标时间最接近(必须大于目标时间) 输出差和按的次数.

题解:将每个时间状态标记,总共3600个状态,搜索即可。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int INF = 999999999;
int a[20];
int vis[4000],tim,step,n,m;
struct Node{
    int step,time;
};

void bfs(){
    queue<Node> q;
    Node s;
    s.step = s.time = 0;
    vis[0] = true;
    q.push(s);
    while(!q.empty()){
        Node now = q.front();
        q.pop();
        if(now.time>=m){
            int temp = now.time - m;
            if(temp==tim){
                step = min(now.step,step);
            }
            if(temp<tim){
                tim = temp;
                step = now.step;
            }
        }
        for(int i=1;i<=n;i++){
            Node next;
            next.step = now.step+1;
            next.time = now.time+a[i];
            if(next.time>3600) next.time = 3600;
            if(next.time>0&&!vis[next.time]){
                vis[next.time] = true;
                q.push(next);
            }
        }
    }
}
int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<=3600;i++) vis[i] = 0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        tim = step = INF;
        if(m==0){
            printf("0 0\n");
            continue;
        }
        bfs();
        printf("%d %d\n",step,tim);
    }
    return 0;
}

 

posted @ 2016-08-24 17:33  樱花庄的龙之介大人  阅读(212)  评论(0编辑  收藏  举报