Little ZuChongzhi's Triangles
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 512000/512000 K(Java/Others)
Total Submission(s): 219 Accepted Submission(s): 118
Problem Description
Zu Chongzhi(429–500) was a prominent Chinese mathematician and astronomer during the LiuSong and Southern Qi Dynasties. Zu calculated the value ofπ to the precision ofsix decimal places and for a thousand years thereafter no subsequentmathematician computed a value this precise. Zu calculated one year as365.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 likedmathematical games. One day, his father gave him some wood sticks as toys. ZuChongzhi found a interesting problem using them. He wanted to make sometriangles by those sticks, and he wanted the total area of all triangles hemade 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 vertexcouldn'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 problemrather than a mathematical problem. You can't solve that problem without acomputer if there are too many sticks. So please bring your computer and goback to Zu's time to help him so that maybe you can change the history.
Input
There are no morethan 10 test cases. For each case:
The first line is an integer N(3 <= N<= 12), indicating the number ofsticks Zu Chongzhi had got. The second line contains N integers, meaning thelength of N sticks. The length of a stick is no more than 100. The input endswith N = 0.
Output
For each testcase, output the maximum total area of triangles Zu could make. Round theresult to 2 digits after decimal point. If Zu couldn't make any triangle, print0.00 .
Sample Input
3
1 1 20
7
3 4 5 3 4 5 90
0
Sample Output
0.00
13.64
题目理解主要就是那三个条件:三根木棍组成三角形、首尾相连、可以不用所有的木棍。其实就是在用一堆木棍去组成三角形,求这些三角形的最大面积和。
思路:
枚举所有能组成的三角形,dfs所搜这些三角形并求出最大面积。
n最大为12,用dfs复杂度为(12*11*10)*(9*8*7)*(6*5*4)*(3*2*1)=12!=479001600,差不多是5*10^8;
应该会超时,所以加了剪枝:
当前的三角形面积加上尽可能多大的面积都比已求出的最大面积小就没有必要搜索下去。
尽可能大的面积:当剩下的木棍的长度和去组成面积最大的图形,显然园形的面积最大。
if(ans>=nowsum+sp*sp/(4.0*3.141592653)) return ;
在杭电上重挂的时候测试93ms;
把剪枝去掉156ms !! 不知道是不是数据不完整。。。。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#define INF 0xfffffff
#define MAX 1000000
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
int n;
double ans;//最大面积
int w[15];
bool vis[15];
double hf(int a, int b, int c)//海伦公式求三角形的面积
{
double p = (a+b+c)*0.5;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
void dfs(double nowsum,int x,double sp) //当前面积,已使用的根数。剩余长度和
{
ans=max(ans,nowsum);
if(x+3>n) return ;//使用超过n根跳出
if(ans>=nowsum+sp*sp/(4.0*3.141592653))//剪枝:当前面积加上剩余的面积都比最大面积小
return ; //不需要搜索直接跳出
for(int i=0; i<n; i++)
if(!vis[i])
for(int j=i+1; j<n; j++)
if(!vis[j])
for(int k=j+1; k<n; k++)
if(!vis[k])
{
if(w[i]+w[j]>w[k]&&w[i]+w[k]>w[j]&&w[j]+w[k]>w[i])//满足构成三角形
{
double tmp=sp-w[i]-w[j]-w[k];
vis[i]=vis[j]=vis[k]=true;//标记已使用
dfs(nowsum+hf(w[i],w[j],w[k]),x+3,tmp);
vis[i]=vis[j]=vis[k]=false;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
double sum=0;
for(int i=0; i<n; i++)
{
scanf("%d",&w[i]);
sum+=w[i];
}
ans=0;
dfs(0,0,sum);
printf("%.2f\n",ans);
}
}