There are n skills. Each skill can only be used once. The monster has m points of blood and asks how many skills are required to kill the monster. Each skill has two values: a and B. a indicates the attack power. B indicates that when the volume of the monster's blood is less than or equal to B, the attack power of this skill can be changed to 2a. A little attack power can kill monsters with a little blood. Simple Deep Search added a pruning from the 421 MS instantly fell to 93 MS code:
#include <stdio.h> #include <string.h> struct node { int M, spell; } a[11]; int hp, n; int vis[11]; int ans ; void dfs(int k, int HP) { int i; if(k>=ans) return ; if(HP<=0) { if(ans>k) ans = k; return; } for(i=1; i<=n; i++) if(!vis[i]) { vis[i] = 1; HP<=a[i].M? dfs(k+1,HP-a[i].spell*2): dfs(k+1,HP-a[i].spell); vis[i] = 0; } } int main() { int i; while(~scanf("%d%d",&n,&hp)) { for(i=1; i<=n; i++) scanf("%d%d",&a[i].spell,&a[i].M); ans = 12; memset(vis,0,sizeof(vis)); dfs(0,hp); if(ans !=12) printf("%d\n",ans); else printf("-1\n"); } return 0; } #include <stdio.h>#include <string.h>struct node { int M, spell;} a[11];int hp, n;int vis[11];int ans ;void dfs(int k, int HP) { int i; if(k>=ans) return ; if(HP<=0) { if(ans>k) ans = k; return; } for(i=1; i<=n; i++) if(!vis[i]) { vis[i] = 1; HP<=a[i].M? dfs(k+1,HP-a[i].spell*2): dfs(k+1,HP-a[i].spell); vis[i] = 0; }}int main() { int i; while(~scanf("%d%d",&n,&hp)) { for(i=1; i<=n; i++) scanf("%d%d",&a[i].spell,&a[i].M); ans = 12; memset(vis,0,sizeof(vis)); dfs(0,hp); if(ans !=12) printf("%d\n",ans); else printf("-1\n"); } return 0;}