hdu2616 Kill the monster

①简单bfs+状压(赤果果)

View Code
 1 #include<stdio.h>
 2 #include<queue>
 3 using namespace std;
 4 
 5 int n,m,data[10][2];
 6 struct node
 7 {
 8     int spell,dist,hp;
 9 };
10 
11 int bfs(void)
12 {
13     node sn={0,0,m};
14     queue<node> Q;
15 
16     Q.push(sn);
17     while( !Q.empty() )
18     {
19         node cur=Q.front();
20 
21         Q.pop();
22         for(int i=0;i<n;i++)
23         {
24             if( cur.spell&(1<<i) )  continue;
25             node next={cur.spell|(1<<i),cur.dist+1,cur.hp-data[i][0]};
26 
27             if( cur.hp<=data[i][1] )    next.hp-=data[i][0];
28             if( next.hp<=0 )    return next.dist;
29             Q.push(next);
30         }
31     }
32     return -1;
33 }
34 
35 int main()
36 {
37     while( ~scanf("%d%d",&n,&m) )
38     {
39         for(int i=0;i<n;i++)
40             scanf("%d%d",&data[i][0],&data[i][1]);
41         printf("%d\n",bfs());
42     }
43     return 0;
44 }
45 /*
46     简单的bfs+状压
47     本来有个state[1024]数组,想剪掉一些肯定不是最优的情况
48     但发现有没有这个数组时间都一样,就舍掉了。
49     于是一个赤果果的bfs诞生了!!!
50 */

②dfs枚举执行顺序 或 直接STL全排列(纯暴力)

View Code
 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int n,m,ans,data[10][2],p[10];
 8 
 9     while( ~scanf("%d%d",&n,&m) )
10     {
11         for(int i=0;i<n;i++)    scanf("%d%d",&data[i][0],&data[i][1]);
12         for(int i=0;i<n;i++)    p[i]=i;
13         ans=m+1;
14         do
15         {
16             int hp=m;
17 
18             for(int i=0;i<n;i++)
19             {
20                 if( hp<=data[p[i]][1] )    hp-=2*data[p[i]][0];
21                 else hp-=data[p[i]][0];
22                 if( hp<=0 )
23                 {
24                     ans=min(ans,i+1);
25                     break;
26                 }
27             }
28         }while( next_permutation(p,p+n) );//STL
29         if( m+1==ans )  puts("-1");
30         else printf("%d\n",ans);
31     }
32     return 0;
33 }
34 /*
35     从ycl那里吸收来的想法,纯暴力枚举排列(即枚举执行顺序,也可以dfs写啦)
36 */

 

posted @ 2012-11-22 21:35  kiwi_bird  阅读(160)  评论(0编辑  收藏  举报