杭电2616Kill the monster(BFS过)(标记结构体解法)
来源:互联网 发布:mac照片在哪里找到 编辑:程序博客网 时间:2024/05/12 13:44
Kill the monster
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1127 Accepted Submission(s): 788
Problem Description
There is a mountain near yifenfei’s hometown. On the mountain lived a big monster. As a hero in hometown, yifenfei wants to kill it.
Now we know yifenfei have n spells, and the monster have m HP, when HP <= 0 meaning monster be killed. Yifenfei’s spells have different effect if used in different time. now tell you each spells’s effects , expressed (A ,M). A show the spell can cost A HP to monster in the common time. M show that when the monster’s HP <= M, using this spell can get double effect.
Now we know yifenfei have n spells, and the monster have m HP, when HP <= 0 meaning monster be killed. Yifenfei’s spells have different effect if used in different time. now tell you each spells’s effects , expressed (A ,M). A show the spell can cost A HP to monster in the common time. M show that when the monster’s HP <= M, using this spell can get double effect.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m (2<n<10, 1<m<10^7), express how many spells yifenfei has.
Next n line , each line express one spell. (Ai, Mi).(0<Ai,Mi<=m).
Each test case include, first two integers n, m (2<n<10, 1<m<10^7), express how many spells yifenfei has.
Next n line , each line express one spell. (Ai, Mi).(0<Ai,Mi<=m).
Output
For each test case output one integer that how many spells yifenfei should use at least. If yifenfei can not kill the monster output -1.
Sample Input
3 10010 2045 895 403 10010 2045 905 403 10010 2045 845 40
Sample Output
32-1
这个题目其实暴力也能解.~但这里还是运用了算法 提高效率~
题目炸一看还以为是背包.(0-1)但是搞了搞发现很难控制血量的伤害是双倍与单倍的问题 ~于是就慢慢放弃了这种解法 还是走上了老路~
搜索.
其实这个题目用深搜还是比较好理解的 而且数据也并不大 一定不会涉及到超时的问题~
~但是捏 我还是比较喜欢广搜来解题~
这里先谈初始化:
struct gongji{ int spell[11],output,hp;//spell数组用来标记这种攻击方式是否已经用过了.output自然是输出.hp表示当前敌人还有的血量.}now,nex;int a[11][2];//a[][0]存单倍伤害值a[][1]存双倍伤害条件
接下来就是bfs的主体了~
int bfs(){ for(int i=0;i<11;i++) { now.spell[i]=0; }//初始化第一个push进去的数组~标记上所有攻击方式都没用过. now.output=0; now.hp=m; queue<gongji>s; s.push(now); int caonima=0; while(!s.empty()) { now=s.front(); s.pop(); for(int i=0;i<n;i++)//这里的操作还是很容易理解的. { if(now.spell[i]==1)continue; nex=now; nex.spell[i]=1; nex.output=now.output+1; if(now.hp<=a[i][1])nex.hp=now.hp-2*a[i][0]; else nex.hp=now.hp-a[i][0]; if(nex.hp<=0)return nex.output; s.push(nex); } } return -1;}
最后贴上AC完整代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int n,m;struct gongji{ int spell[11],output,hp;}now,nex;int a[11][2];int bfs(){ for(int i=0;i<11;i++) { now.spell[i]=0; } now.output=0; now.hp=m; queue<gongji>s; s.push(now); int caonima=0; while(!s.empty()) { now=s.front(); s.pop(); for(int i=0;i<n;i++) { if(now.spell[i]==1)continue; nex=now; nex.spell[i]=1; nex.output=now.output+1; if(now.hp<=a[i][1])nex.hp=now.hp-2*a[i][0]; else nex.hp=now.hp-a[i][0]; if(nex.hp<=0)return nex.output; s.push(nex); } } return -1;}int main(){ while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++) { scanf("%d%d",&a[i][0],&a[i][1]); } printf("%d\n",bfs()); }}
0 0
- 杭电2616Kill the monster(BFS过)(标记结构体解法)
- 杭电 2616 Kill the monster
- HDOJ 2616 Kill the monster(DFS+BFS)
- 杭电ACM HDU 2616 Kill the monster
- hdu 2616 Kill the monster(深搜)
- hdu 2616 Kill the monster(DFS)
- hdu 2616 Kill the monster(暴搜)
- hdu 2616 Kill the monster(dfs)
- Kill the monster(深搜)
- HDU 2616 Kill the monster (深搜DFS)
- ACdream Kill The Monster(线性dp)
- A - Kill the monster (DFS)
- ACdream 1072 Kill The Monster (dp)
- (DFS) Kill the monster -- HDOJ
- hdu 2616 Kill the monster
- HDU 2616 Kill the monster
- HDU 2616 Kill the monster
- hdu(2616)Kill the monster
- JQuery获取与设置HTML元素的内容或文本的实现代码
- BZOJ 3426 && CodeChef/CHANGE
- session过期时间
- 诸子百家
- 频繁分配释放内存导致的性能问题的分析
- 杭电2616Kill the monster(BFS过)(标记结构体解法)
- JavaScript(19)jQuery HTML 获取和设置内容和属性
- 升级Xcode 7 各种插件无法使用解决方法
- C /C++标准库 - <cstdio>(stdio.h)
- day02注释 进制 数据类型 常量 变量(上)
- cannot find class for bean with name ''
- 列表选择编辑风格-报表系统
- EEPROM和FLASH的区别
- Linux 文件系统结构介绍