码迷,mamicode.com
首页 > 其他好文 > 详细

HDU5135 dfs搜索 枚举种数

时间:2017-11-26 21:55:46      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:eof   tick   div   面积   uri   class   main   sam   ack   

 

Little Zu Chongzhi‘s Triangles

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 2195    Accepted Submission(s): 1262


Problem Description
Zu Chongzhi (429–500) was a prominent Chinese mathematician and astronomer during the Liu Song and Southern Qi Dynasties. Zu calculated the value ofπ to the precision of six decimal places and for a thousand years thereafter no subsequent mathematician computed a value this precise. Zu calculated one year as 365.24281481 days, which is very close to 365.24219878 days as we know today. He also worked on deducing the formula for the volume of a sphere. 

It is said in some legend story books that when Zu was a little boy, he liked mathematical games. One day, his father gave him some wood sticks as toys. Zu Chongzhi found a interesting problem using them. He wanted to make some triangles by those sticks, and he wanted the total area of all triangles he made to be as large as possible. The rules were :

1) A triangle could only consist of 3 sticks.
2) A triangle‘s vertexes must be end points of sticks. A triangle‘s vertex couldn‘t be in the middle of a stick.
3) Zu didn‘t have to use all sticks.

Unfortunately, Zu didn‘t solve that problem because it was an algorithm problem rather than a mathematical problem. You can‘t solve that problem without a computer if there are too many sticks. So please bring your computer and go back to Zu‘s time to help him so that maybe you can change the history.
 

 

Input
There are no more than 10 test cases. For each case:

The first line is an integer N(3 <= N<= 12), indicating the number of sticks Zu Chongzhi had got. The second line contains N integers, meaning the length of N sticks. The length of a stick is no more than 100. The input ends with N = 0.
 

 

Output
For each test case, output the maximum total area of triangles Zu could make. Round the result to 2 digits after decimal point. If Zu couldn‘t make any triangle, print 0.00 .
 

 

Sample Input
3
1 1 20
7
3 4 5 3 4 5 90
0
 
Sample Output
0.00
13.64

题意  n条边 组成三角形使面积和尽可能大

解析  由于n比较小加上数据有点水 可以枚举所有情况 进行计算  

注 枚举过程情况会有重复 但时间上还是过了   相当于  先从n个中 拿n/3*3条边 再从n/3*3中拿3条边 再从(n/3-1)*3里面再拿3条边。。。用组合数计算得到的种数重复情况是一样的 

我在说什么QAQ   我有点懵X 不要看了 当我没说 打扰了

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <vector>
11 using namespace std;
12 const int maxn= 100;
13 const double eps= 1e-6;
14 const int inf = 0x3f3f3f3f;
15 const int mod =3;
16 typedef long long ll;
17 typedef long double ld;
18 int n,kase;
19 double ans;
20 int visit[maxn];
21 double a[maxn];
22 double judge(double x,double y,double z) //返回当前三条边 组成三角形的面积
23 {
24     if(x>y)                  
25         swap(x,y);
26     if(x>z)
27         swap(x,z);
28     if(y>z)
29         swap(y,z);
30     if(x+y<=z)                   //不能组成三角形 返回0
31         return 0.0;
32     else
33     {
34         double p=(x+y+z)/2;       //海伦公式
35         return sqrt(p*(p-x)*(p-y)*(p-z));
36     }
37 }
38 void dfs(int x,double y) //当前枚举x个三角形 总面积为y
39 {
40     ans=max(ans,y);
41     if(x==n/3)
42     {
43         x=0;
44         //printf("%d %lf\n",kase++,y);
45         y=0.0;
46     }
47     for(int i=1; i<=n; i++)
48     {
49         if(visit[i]==1)
50             continue;
51         for(int j=i+1; j<=n; j++)
52         {
53             if(visit[j]==1)
54                 continue;
55             for(int k=j+1; k<=n; k++)
56             {
57                 if(visit[k]==1)
58                     continue;
59                 else
60                 {
61                     visit[i]=1,visit[j]=1,visit[k]=1;
62                     dfs(x+1,y+judge(a[i],a[j],a[k]));
63                     visit[i]=0,visit[j]=0;visit[k]=0;
64                 }
65             }
66         }
67     }
68 }
69 int main()
70 {
71     while(scanf("%d",&n)!=EOF&&n)
72     {
73         for(int i=1;i<=n;i++)
74         {
75             scanf("%lf",&a[i]);
76         }
77         memset(visit,0,sizeof(visit));
78         ans=0.0;
79         //kase=1;
80         dfs(0,0.0);
81         printf("%.2lf\n",ans);
82     }
83 }

 

HDU5135 dfs搜索 枚举种数

标签:eof   tick   div   面积   uri   class   main   sam   ack   

原文地址:http://www.cnblogs.com/stranger-/p/7900409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!