Button Bashing

本文介绍了一种通过按钮组合实现特定时间设定的算法。利用简单的广度优先搜索(BFS)策略,在限定范围内找到最短步骤及超时情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

暑假集训二 Button Bashing

题目来源:http://www.sizaif.com:6680/upload/file/20180717/20180717133439_18137.pdf

题目大意:有一个微波炉运行时间在0-3600秒之内,它有n个按钮,每个按钮代表一个时间即按下按钮后会增加的时间(可能为负数),首先给定t组测试事例(最多100),每个测试事例给定两个数n,m,分别表示按钮的个数和你需要计算的一个时间,下面一行n个数即每个按钮按下后增加的时间,现在你需要计算出达到给定时间m需要按下的次数,以及如果不能正好到达该时间所超出的时间。

分析:简单bfs,找出由给定的n个时间组成的 所有时间的最少的次数。

代码如下:

#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int a[30],b[3700];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(b,INF,sizeof(b));
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        queue<int>q;
        q.push(0);
        b[0]=0;
        while(!q.empty())
        {
            int ff=q.front();//first in first out
            q.pop();
            for(int i=0;i<n;i++)
            {
                int ss=ff+a[i];
                if(ss<0)
                    ss=0;
                else if(ss>3600)
                    ss=3600;
                if(b[ss]<=b[ff]+1)//表示面已有ss的路径
                    continue;//如果是则跳过下面的代码,进入下个循环
                q.push(ss);
                b[ss]=b[ff]+1;
            }
        }
        int k;
        for(int k=m;k<=3600;k++)
        {
            if(b[k]!=INF)
            {
                printf("%d %d\n",b[k],k-m);
                break;
            }
        }
    }
    return 0;
}

看了好几个题解才写的……

<?xml version="1.0" encoding="utf-8" ?> <Defs> <ThingDef ParentName="BaseMeleeWeapon_Blunt"> <defName>MeleeWeapon_Club</defName> <label>club</label> <description>A shaped stick, heavy at one end, for bashing enemies to death. This weapon is so old it's part of human biology. Evolutionary biologists say that a major reason humans can tilt our wrists side-to-side (instead of just forward-and-back like other primates) is to be able to better swing a club.</description> <graphicData> <texPath>Things/Item/Equipment/WeaponMelee/Club</texPath> <graphicClass>Graphic_Single</graphicClass> </graphicData> <uiIconScale>1.1</uiIconScale> <techLevel>Neolithic</techLevel> <weaponTags> <li>NeolithicMeleeBasic</li> </weaponTags> <weaponClasses> <li>Neolithic</li> </weaponClasses> <costStuffCount>40</costStuffCount> <statBases> <WorkToMake>1200</WorkToMake> <Mass>2</Mass> </statBases> <equippedAngleOffset>-65</equippedAngleOffset> <stuffCategories> <li>Metallic</li> <li>Woody</li> <li>Stony</li> </stuffCategories> <recipeMaker> <recipeUsers> <li>CraftingSpot</li> </recipeUsers> <displayPriority>300</displayPriority> </recipeMaker> <tools> <li> <label>handle</label> <capacities> <li>Poke</li> </capacities> <power>9</power> <cooldownTime>2</cooldownTime> </li> <li> <label>head</label> <labelUsedInLogging>false</labelUsedInLogging> <capacities> <li>Blunt</li> </capacities> <power>14</power> <cooldownTime>2</cooldownTime> </li> </tools> </ThingDef> <ThingDef ParentName="BaseMeleeWeapon_Sharp_Quality"> <defName>MeleeWeapon_Knife</defName> <label>knife</label> <techLevel>Neolithic</techLevel> <description>One of mankind's oldest manufactured objects. The knife has taken many forms, but the concept has always remained the same: A handle for holding and a sharp edge for cutting.</description> <graphicData> <texPath>Things/Item/Equipment/WeaponMelee/Knife</texPath> <graphicClass>Graphic_Single</graphicClass> </graphicData> <uiIconScale>1.6</uiIconScale> <weaponTags> <li>MedievalMeleeBasic</li> <li>NeolithicMeleeBasic</li> </weaponTags> <weaponClasses> <li>Neolithic</li> </weaponClasses> <costStuffCount>30</costStuffCount> <statBases> <WorkToMake>1800</WorkToMake> <Mass>0.5</Mass> </statBases> <equippedAngleOffset>-65</equippedAngleOffset> <stuffCategories> <li>Metallic</li> </stuffCategories> <burnableByRecipe>false</burnableByRecipe> <tools> <li> <label>handle</label> <capacities> <li>Blunt</li> </capacities> <power>9</power> <cooldownTime>2</cooldownTime> </li> <li> <label>blade</label> <capacities> <li>Cut</li> </capacities> <power>12</power> <cooldownTime>1.5</cooldownTime> </li> <li> <label>point</label> <capacities> <li>Stab</li> </capacities> <power>13</power> <cooldownTime>2</cooldownTime> </li> </tools> <recipeMaker> <recipeUsers> <li>CraftingSpot</li> </recipeUsers> <displayPriority>310</displayPriority> </recipeMaker> </ThingDef> <ThingDef ParentName="BaseMeleeWeapon_Sharp_Quality"> <defName>MeleeWeapon_Ikwa</defName> <label>ikwa</label> <description>A short spearhandle attached to a long bladed point, designed to be driven into vital organs with a quick thrust.</description> <graphicData> <texPath>Things/Item/Equipment/WeaponMelee/Ikwa</texPath> <graphicClass>Graphic_Single</graphicClass> </graphicData> <uiIconScale>1.25</uiIconScale> <techLevel>Neolithic</techLevel> <weaponTags> <li>NeolithicMeleeDecent</li> </weaponTags> <weaponClasses> <li>Neolithic</li> </weaponClasses> <costStuffCount>50</costStuffCount> <statBases> <WorkToMake>5000</WorkToMake> <Mass>1.1</Mass> </statBases> <equippedAngleOffset>-25</equippedAngleOffset> <stuffCategories> <li>Metallic</li> <li>Woody</li> </stuffCategories> <tools> <li> <label>handle</label> <capacities> <li>Blunt</li> <li>Poke</li> </capacities> <power>9</power> <cooldownTime>2</cooldownTime> </li> <li> <label>head</label> <capacities> <li>Stab</li> </capacities> <power>15</power> <cooldownTime>2</cooldownTime> </li> <li> <label>edge</label> <capacities> <li>Cut</li> </capacities> <power>15</power> <cooldownTime>2</cooldownTime> </li> </tools> <recipeMaker> <researchPrerequisite>Smithing</researchPrerequisite> <skillRequirements> <Crafting>3</Crafting> </skillRequirements> <displayPriority>320</displayPriority> </recipeMaker> </ThingDef> <ThingDef ParentName="BaseMeleeWeapon_Sharp_Quality"> <defName>MeleeWeapon_Spear</defName> <label>spear</label> <description>A polearm tipped with a sharp point for stabbing opponents.</description> <graphicData> <texPath>Things/Item/Equipment/WeaponMelee/Spear</texPath> <graphicClass>Graphic_Single</graphicClass> </graphicData> <techLevel>Neolithic</techLevel> <weaponTags> <li>NeolithicMeleeAdvanced</li> <li>MedievalMeleeAdvanced</li> <li>Spear</li> </weaponTags> <weaponClasses> <li>Neolithic</li> </weaponClasses> <costStuffCount>75</costStuffCount> <thingSetMakerTags><li>RewardStandardQualitySuper</li></thingSetMakerTags> <statBases> <WorkToMake>12000</WorkToMake> <Mass>2</Mass> </statBases> <equippedAngleOffset>-25</equippedAngleOffset> <stuffCategories> <li>Metallic</li> <li>Woody</li> </stuffCategories> <tools> <li> <label>shaft</label> <capacities> <li>Blunt</li> <li>Poke</li> </capacities> <power>13</power> <cooldownTime>2.6</cooldownTime> </li> <li> <label>point</label> <capacities> <li>Stab</li> </capacities> <power>23</power> <armorPenetration>0.50</armorPenetration> <cooldownTime>2.6</cooldownTime> </li> </tools> <recipeMaker> <researchPrerequisite>LongBlades</researchPrerequisite> <skillRequirements> <Crafting>4</Crafting> </skillRequirements> <displayPriority>330</displayPriority> </recipeMaker> </ThingDef> </Defs> 逐行解释
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值