HDU5135 dfs搜索 枚举种数

时间:2022-03-06 23:04:27

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
 
Sample Output
0.00
13.64

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

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

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

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

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
using namespace std;
const int maxn= ;
const double eps= 1e-;
const int inf = 0x3f3f3f3f;
const int mod =;
typedef long long ll;
typedef long double ld;
int n,kase;
double ans;
int visit[maxn];
double a[maxn];
double judge(double x,double y,double z) //返回当前三条边 组成三角形的面积
{
if(x>y)
swap(x,y);
if(x>z)
swap(x,z);
if(y>z)
swap(y,z);
if(x+y<=z) //不能组成三角形 返回0
return 0.0;
else
{
double p=(x+y+z)/; //海伦公式
return sqrt(p*(p-x)*(p-y)*(p-z));
}
}
void dfs(int x,double y) //当前枚举x个三角形 总面积为y
{
ans=max(ans,y);
if(x==n/)
{
x=;
//printf("%d %lf\n",kase++,y);
y=0.0;
}
for(int i=; i<=n; i++)
{
if(visit[i]==)
continue;
for(int j=i+; j<=n; j++)
{
if(visit[j]==)
continue;
for(int k=j+; k<=n; k++)
{
if(visit[k]==)
continue;
else
{
visit[i]=,visit[j]=,visit[k]=;
dfs(x+,y+judge(a[i],a[j],a[k]));
visit[i]=,visit[j]=;visit[k]=;
}
}
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
for(int i=;i<=n;i++)
{
scanf("%lf",&a[i]);
}
memset(visit,,sizeof(visit));
ans=0.0;
//kase=1;
dfs(,0.0);
printf("%.2lf\n",ans);
}
}