HDU 2616 Kill the monster
来源:互联网 发布:mac os x 10.11.6 iso 编辑:程序博客网 时间:2024/05/16 07:37
代码dfs:
#include<iostream>#include<cstdio>using namespace std;int n,m;int ans;struct node{ int ai,mi;};node PH[15];bool vis[15];void dfs(int x,int t){ if(x>=ans) return ; if(t<=0) { if(ans>x) ans=x; return ; } for(int i=0;i<n;i++) { if(!vis[i]) { vis[i]=true; if(t<=PH[i].mi) { dfs(x+1,t-2*PH[i].ai); } else if(t>PH[i].mi) { dfs(x+1,t-PH[i].ai); } vis[i]=false; } }}int main(){ while(scanf("%d%d",&n,&m)==2) { for(int i=0;i<n;i++) { scanf("%d%d",&PH[i].ai,&PH[i].mi); vis[i]=false; } ans=n+1; dfs(0,m); if(ans!=n+1) printf("%d\n",ans); else printf("-1\n"); } return 0;}
代码:
因为数据比较小,所以可以用next_permutation全排列函数
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int n,m;struct node{ int ai,mi;};node PH[15];int vis[15];int main(){ while(scanf("%d%d",&n,&m)==2) { int min=150; for(int i=0;i<n;i++) { scanf("%d%d",&PH[i].ai,&PH[i].mi); } for(int i=0;i<n;i++) vis[i]=i; do { int count=0; int t=m; for(int i=0;i<n;i++) { count++; if(t<=PH[vis[i]].mi) t-=2*PH[vis[i]].ai; else t-=PH[vis[i]].ai; if(t<=0) { if(count<min) min=count; break; } } }while(next_permutation(vis,vis+n)); if(min==150) printf("-1\n"); else printf("%d\n",min); } return 0;}
- hdu 2616 Kill the monster
- HDU 2616 Kill the monster
- HDU 2616 Kill the monster
- hdu(2616)Kill the monster
- HDU 2616 Kill the monster
- hdu 2616 Kill the monster
- HDU-2616-Kill the monster
- HDU 2616 Kill the monster
- hdu 2616 Kill the monster(深搜)
- hdu 2616 Kill the monster(DFS)
- hdu 2616 Kill the monster(暴搜)
- 【DFS】hdu 2616 Kill the monster
- HDU 2616 Kill the monster 简单DFS
- hdu 2616 Kill the monster(dfs)
- 杭电ACM HDU 2616 Kill the monster
- hdu 2616 Kill the monster(简单回溯搜索)
- HDU 2616 Kill the monster (深搜DFS)
- 杭电 2616 Kill the monster
- 关于括号匹配的一些问题总结
- NYOJ 题目23 取石子(一),hdu 题目1846 Brave Game 巴什博奕(Bash Game)
- 深入Java集合学习系列:ArrayList的实现原理
- 函数
- js对象
- HDU 2616 Kill the monster
- js对象专用语句
- Resource Planning Application in SAP CRM
- TOJ 1132 ZOJ 1090 The Circumference of the Circle
- 正则表达式 java 整理
- js数组
- cookie/session总结
- c++的I/O(vc版)(一)
- IE6不得不说的那些BUG