首页 > 代码库 > uva 10012

uva 10012

题目意思:  给定m个圆的半径,现在要求找到一个矩形使得每一个球都以地面相切,要求输出最小的矩阵的长度

 1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <cmath> 7 using namespace std; 8 int num; 9 bool vis[10];10 double a[8], c[8], l[8], _max;11 double sqr(double r)12 {13     return r*r;14 }15 16 void dfs(int cnt, double len)17 {18     if(len > _max)19         return ;20     if(cnt == num)21     {22         double ll = c[0];23         len += c[cnt - 1];24         for(int i = 0; i < num - 1; i++)25             if(l[i] + c[i] > len)26                 len = l[i] + c[i];27         for(int i = 1; i < num; i++)28             if(c[i] - l[i] > ll)29                 ll = c[i] - l[i];30         len += ll;31         if(len < _max)32             _max = len;33         return ;34     }35     for(int i = 0; i < num; i++)36     {37         if(vis[i]) continue;38         vis[i] = true;39         c[cnt] = a[i];40         l[cnt] = c[cnt];41         for(int j = cnt - 1; j >= 0; j--)42         {43             double dis = l[j] + sqrt(sqr(c[j]+c[cnt])-sqr(c[j]-c[cnt]));44             if(dis > l[cnt])45                 l[cnt] = dis;46         }47         dfs(cnt + 1, l[cnt]);48         vis[i] = false;49     }50 }51 int main()52 {53     int n;54     scanf("%d", &n);55     memset(vis, 0, sizeof(vis));56     while(n--)57     {58         _max = 0x7FFFFFFF;59         scanf("%d", &num);60         for(int i = 0; i < num; i++)61             scanf("%lf", &a[i]);62         for(int i = 0; i < num; i++)63         {64             vis[i] = true;65             l[0] = 0;66             c[0] = a[i];67             dfs(1, 0);68             vis[i] = false;69         }70         printf("%.3lf\n", _max);71     }72     return 0;73 }
View Code

 

uva 10012