uva 10012 - How Big Is It?

来源:互联网 发布:数据流程图的符号包括 编辑:程序博客网 时间:2024/05/16 15:23
  How Big Is It? 
Ian's going to California, and he has to pack his things, including his collection of circles. Given a set of circles, your program must find the smallest rectangular box in which they fit. All circles must touch the bottom of the box. The figure below shows an acceptable packing for a set of circles (although this may not be the optimal packing for these particular circles). Note that in an ideal packing, each circle should touch at least one other circle (but you probably figured that out).

Input 

The first line of input contains a single positive decimal integer n, n<=50. This indicates the number of lines which follow. The subsequent n lines each contain a series of numbers separated by spaces. The first number on each of these lines is a positive integerm, m<=8, which indicates how many other numbers appear on that line. The nextm numbers on the line are the radii of the circles which must be packed in a single box. These numbers need not be integers.

Output 

For each data line of input, excluding the first line of input containing n, your program must output the size of the smallest rectangle which can pack the circles. Each case should be output on a separate line by itself, with three places after the decimal point. Do not output leading zeroes unless the number is less than 1, e.g.0.543.

Sample Input 

33 2.0 1.0 2.04 2.0 2.0 2.0 2.03 2.0 1.0 4.0

Sample Output 

9.65716.00012.657


最多8个圆,考虑到左右对称如果是按顺序生成全排列最最多8!/2;

难点在于圆与圆之间不一定直接相切,

假设两圆半径分别为x,y且直接相切

圆心距为(x,y相对大小不影响结果) sqrt((x+y)*(x+y)-(x-y)*(x-y))=2*sqrt(x*y);

如果两个圆不相切,仍然按照这样算结果小于实际的两圆圆心距;

 

设第n个圆心坐标为Rn,r(x,y) 为第x个圆与第y个圆按相切公式算出来的的圆心距(x,y)不一定实际相切

于是我们可以假设则圆心横坐标R1,第二个圆的圆心坐标R2=R1+r(1,2),第三个圆MAX(R1+r(1,3),R2+r(2,3)).

用到前面的结论:实际不相切的按公式计算距离偏小,求解第n个圆心坐标是分别假设它与之前的所有圆相切按公式算出来坐标值最大的即为实际相切的;

 

做到这里很容易误以为,距离就是第n个圆加上他的半径,因为我们刚才是假设的初始坐标,而且第一个圆和最后一个圆不一定和矩形的边相切,要用每个圆心坐标和半径算出,所有圆最右和最左到达的位置,相减算出长度;

#include<stdio.h>#include<math.h>double r[10],R[10],max,k,x[10];int n,visit[10];int dfs(int m){int i,j; double pos,left,right; if (m>n) {  x[1]=R[1];  for (i=2;i<=n;i++)  {x[i]=0;   for (j=1;j<i;j++)   {    pos=2*sqrt(R[i]*R[j])+x[j];    if (pos>x[i]) x[i]=pos;   }  }  left=99999999; right=-99999999;  //数据有点大,初始化为999999都wrong  for (i=1;i<=n;i++)  {if (x[i]-R[i]<left)  left=x[i]-R[i];     if (x[i]+R[i]>right) right=x[i]+R[i];  }  if (right-left<max) max=right-left;return 0; } for (i=1;i<=n;i++) if (visit[i]) {visit[i]=0;   R[m]=r[i];  dfs(m+1);     visit[i]=1; }};int main(){int i,t; scanf("%d",&t); while (t--) {  scanf("%d",&n);    for (i=1;i<=n;i++)      {visit[i]=1;       scanf("%lf",&r[i]);      }    max=99999999;    dfs(1);  printf("%.3lf\n",max); } return 0;}


 

 

原创粉丝点击