HDU5135 dfs搜索 枚举种数

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 }
时间: 2024-10-13 08:00:58

HDU5135 dfs搜索 枚举种数的相关文章

hdu1455 dfs搜索之凑棍子

原题地址 这道题和poj的拯救少林神棍是一样的题目. 要用给出的小棍凑成等长的棍子,求能凑成的棍子的最小长度. 直观的包里思路就是枚举所有可能的长度,然后不停的测试小棍组合,先把小棍加入组合,然后不合适就推翻这一根小棍,再测试下一个小棍,直到推翻所有的小棍. 在枚举的时候,我们只需从最长的小棍长,枚举到小棍总长的一半就行了.然后如果再不符合的话,那么就说明所有小棍只能组合成一根棍子了. 我原先看过关于poj上拯救少林神棍这道题目的详细讲解.一个DFS搜索题,这里DFS共有四种剪枝方案: 所给出的

POJ 2965 The Pilots Brothers&#39; refrigerator 搜索+枚举

Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to open a refrigerator. There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refri

HDU 5024 (广州网络赛) Wang Xifeng&#39;s Little Plot 记忆化搜索+枚举

Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin.

搜索枚举

搜索枚举,就是用搜索代替枚举,可以使算法时间复杂度降低,也可以帮助解决很多枚举(暴力)的算法无法解决(或很慢才能解决)的问题. 基本搜索枚举 有如下方程:\[a_1 x_1 + a_2 x_2 + a_3 x_3 + \cdots + a_n x_n = 0\] (\(2 \le n \le 10, 1 \le a_i \le 5, -2 \le x_i \le 2, x_i \in \mathbb{Z}\)) 求其解的总数. int ans = 0; void dfs(int dep, int

hdu1372 dfs搜索之国际象棋的马

原题地址 题意 一个8x8的国际象棋棋盘,你有一个棋子"马".算出棋子"马"从某一格到另一格子的最少步数. 与普通dfs不同的是,你能走的路线不是上下左右,四个方向.而是由"日" 字组成的8个方向.虽然是国际象棋的马,但是其实和中国象棋的马走法还是一样的. 代码 #include<iostream> #include<cstdio> #include<cstring> using namespace std;

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

URAL 1298 knight dfs搜索

1298. Knight Time limit: 2.0 second Memory limit: 64 MB Even paratroopers have vacations. The flight to Sirius in the depths of "The Admiral Brisco" Leo Hao whiled away with chessboard. No, he did not like usual chess game, and in addition, he d

nyist oj 19 擅长排列的小明(dfs搜索+STL)

擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长.现在需要你写一个程序来验证擅长排列的小明到底对不对. 输入 第一行输入整数N(1<N<10)表示多少组测试数据, 每组测试数据第一行两个整数 n m (1<n<9,0<m<=n) 输出 在1-n中选

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end