xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····
http://acm.pku.edu.cn/JudgeOnline/problem?id=2362
DFS训练题
 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 vector<int> sticks;
 7 int mark[25];
 8 int s;
 9 int dfs(int val,int h,int cnt)
10 {
11     for(int i=h;i<sticks.size();++i){
12         if(mark[i]==0){
13             mark[i]=1;
14             if(val+sticks[i]==s){
15                 cnt++;
16                 if(cnt==4)return 1;
17                 int ans=dfs(0,1,cnt);
18                 if(ans==1)return 1;
19                 mark[i]=0;
20                 return 0;
21             }
22             else if(val+sticks[i]<s){
23                 int ans=dfs(val+sticks[i],i,cnt);
24                 if(ans==1)return 1;
25                 mark[i]=0;
26             }
27             else if(val+sticks[i]>s){
28                 mark[i]=0;
29                 return 0;
30             }
31         }
32         if(h>0&&mark[0]==0)return 0;
33     }
34     return 0;
35 }
36 int main()
37 {
38     int c;
39     scanf("%d",&c);
40     while(c--){
41         memset(mark,0,sizeof(mark));
42         sticks.clear();
43         int num;
44         int k;
45         scanf("%d",&k);
46         int sum=0;
47         int tmp=0;
48         while(k--){
49             scanf("%d",&num);
50             sticks.push_back(num);
51             sum+=num;
52             if(num>tmp)tmp=num;
53         }
54         if(sum%4!=0||sticks.size()<4){
55             printf("no\n");
56             continue;
57         }
58         s=sum/4;
59         if(s<tmp){
60             printf("no\n");
61             continue;
62         }
63         sort(sticks.begin(),sticks.end());
64         int ans=dfs(0,0,1);
65         if(ans){
66             printf("yes\n");
67         }
68         else printf("no\n");
69     }
70     return 0;
71 }

posted on 2008-07-24 20:56 小果子 阅读(325) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理