hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)

N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的

Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output
6
5

 

 1 # include <cstdio>
 2 # include <cmath>
 3 # include <iostream>
 4 # include <cstring>
 5 # include <algorithm>
 6 using namespace std ;
 7 
 8 int sum , num;
 9 int a[70] ;
10 bool v[70] ;
11 int len , n;
12 
13 bool cmp(const int &x , const int &y)
14 {
15     return x > y ;
16 }
17 
18 bool dfs(int count , int L , int pos) //已完成的数量  当前木棒长度  位置
19 {
20     if (len == sum)
21         return 1 ;
22     if (count == num)
23         return 1 ;
24     for (int i = pos ; i < n ; i++)
25     {
26         if (v[i])
27             continue ;
28         if (L + a[i] == len)
29         {
30             v[i] = 1 ;
31             if (dfs(count+1 , 0 , 0))
32                 return 1 ;
33             v[i] = 0 ;
34             return 0 ;
35         }
36         else if (L + a[i] < len)
37         {
38             v[i] = 1 ;
39             if (dfs(count , L+a[i] , i+1))
40                 return 1 ;
41             v[i] = 0 ;
42             if (L == 0)  //说明有一根木棒没有派上用场
43                 return 0 ;
44             while (a[i] == a[i+1])  //如果下一根的长度和这根一样 则继续搜索下面的
45                 i++ ;
46         }
47     }
48     return 0 ;
49 }
50 
51 int main()
52 {
53     //freopen("in.txt","r",stdin) ;
54     while (scanf("%d" , &n) , n)
55     {
56         sum = 0 ;
57         int i ;
58         for (i = 0 ; i < n ; i++)
59         {
60             scanf("%d" , &a[i]) ;
61             sum += a[i] ;
62         }
63         sort(a,a+n,cmp) ;
64         if (a[0] > sum - a[0])  //如过第1根木棒比剩下的木棒和 还要长
65         {
66             printf("%d\n" , sum) ;
67             continue ;
68         }
69         for (len = a[0] ; len <= sum ; len++) //一根完整木棒的长度肯定大于最长的小木棒
70         {
71             if (sum % len)
72                 continue ;
73             memset(v , 0 , sizeof(v)) ;
74             num = sum/len ;
75             if (dfs(0,0,0))
76             {
77                 printf("%d\n" , len) ;
78                 break ;
79             }
80         }
81     }
82     return 0 ;
83 
84 }
View Code

 

hdu 1518 N根木棒 能否拼成正方形  

 

Sample Input
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5

 

Sample Output
yes
no
yes

 

 1 # include <cstdio>
 2 # include <cmath>
 3 # include <iostream>
 4 # include <cstring>
 5 # include <algorithm>
 6 using namespace std ;
 7 
 8 int sum ;
 9 int a[70] ;
10 bool v[70] ;
11 int len , n;
12 
13 bool cmp(const int &x , const int &y)
14 {
15     return x > y ;
16 }
17 
18 bool dfs(int count , int L , int pos) //已完成的数量  当前木棒长度  位置
19 {
20 
21     if (count == 4)
22         return 1 ;
23     for (int i = pos ; i < n ; i++)
24     {
25         if (v[i])
26             continue ;
27         if (L + a[i] == len)
28         {
29             v[i] = 1 ;
30             if (dfs(count+1 , 0 , 0))
31                 return 1 ;
32             v[i] = 0 ;
33             return 0 ;
34         }
35         else if (L + a[i] < len)
36         {
37             v[i] = 1 ;
38             if (dfs(count , L+a[i] , i+1))
39                 return 1 ;
40             v[i] = 0 ;
41             if (L == 0)  //说明有一根木棒没有派上用场
42                 return 0 ;
43             while (a[i] == a[i+1])  //如果下一根的长度和这根一样 则继续搜索下面的
44                 i++ ;
45         }
46     }
47     return 0 ;
48 }
49 
50 int main()
51 {
52    // freopen("in.txt","r"a,stdin) ;
53     int T ;
54     scanf("%d" , &T) ;
55     while (T--)
56     {
57         scanf("%d" , &n) ;
58         sum = 0 ;
59         int i ;
60         for (i = 0 ; i < n ; i++)
61         {
62             scanf("%d" , &a[i]) ;
63             sum += a[i] ;
64         }
65         if (sum%4)
66         {
67             printf("no\n") ;
68             continue ;
69         }
70         sort(a,a+n,cmp) ;
71         len  = sum / 4 ;
72         if (a[0] > len)
73         {
74             printf("no\n") ;
75             continue ;
76         }
77         memset(v,0,sizeof(v)) ;
78         if (dfs(0,0,0))
79         {
80             printf("yes\n") ;
81         }
82         else
83             printf("no\n") ;
84 
85 
86     }
87     return 0 ;
88 
89 }
View Code

 

posted @ 2015-05-17 23:46  __Meng  阅读(403)  评论(0编辑  收藏  举报