UVA 10012 How Big is It?

Source: Internet
Author: User
Tags pack touch

Original question:
Ian's going to California, and he had 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 is not being 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 it 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 the these lines are a positive integer m, m≤8, which indicates how many other numbers appear on t Hat line. The next m numbers on the line is the radii of the circles which must is 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 Smal Lest rectangle which can pack the circles. Each case should is output on a separate line by itself, with three places after the decimal point. Do not output leading zeroes unless the number was less than 1, e.g. 0.543.
Sample Input
3
3 2.0 1.0 2.0
4 2.0 2.0) 2.0 2.0
3 2.0 1.0 4.0
Sample Output
9.657
16.000
12.657

English:
There's a man going on a trip to take a bunch of rings, now give you the radius of these rings and ask how you put them, so that the length of the box you need is minimal.

#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
const double inf=99999999;
Double circle[9],ans,tmp,x[9];
int n;
    Double My_abs (double x) {if (x<0) return-x;
return x;
    } double Get_dis (int c1,int c2) {double A=CIRCLE[C1]+CIRCLE[C2];
    Double B=my_abs (CIRCLE[C1]-CIRCLE[C2]);
return sqrt (a*a-b*b);
        }//The circle tangent to the I circle must be the X-coordinate farthest from the void solve () {do {memset (x,-1,sizeof (X));
        X[1]=CIRCLE[1];
        Tmp=-1;
            for (int i=2;i<=n;i++) {double x_=circle[i];
            for (int j=1;j<i;j++)//Find the largest X_=max (X_,x[j]+get_dis (i,j));
        X[i]=x_;
}//cout<<endl;
for (int i=1;i<=n;i++)//cout<<x[i]<< "";
cout<<endl;
for (int i=1;i<=n;i++)//cout<<circle[i]<< "";
        cout<<endl;
for (int i=1;i<=n;i++) Tmp=max (Tmp,x[i]+circle[i]);     //   Cout<<fixed<<setprecision (3) <<tmp<<endl;
    Ans=min (ANS,TMP);
} while (Next_permutation (circle+1,circle+1+n));
    } int main () {Ios::sync_with_stdio (false);
    int t;
    cin>>t;
        while (t--) {cin>>n;
        for (int i=1;i<=n;i++) cin>>circle[i];
        Ans=inf;
        Sort (circle+1,circle+1+n);
        Solve ();
    Cout<<fixed<<setprecision (3) <<ans<<endl;
} return 0; }//3 8 1 2//

Solution:
Just a look at this problem feel quite simple, direct search or with the formation of the arrangement chant. As a result, a lot of holes were found on the paper.

First of all, the simplest case, if the radius of the circle is not small, then the direct order of the line.

Second, there is a relatively large radius of the circle, then the small circle can be stuffed into the big circle and the bottom of the "gap".

Third, if there is a relatively large radius of the circle, the small circle of the circle "against the Wall", then the big circle will be "against the wall."

After considering these three situations, it is easy to think of the brute force + greedy strategy, that is, each time you enumerate the order of the circle, and then paste the circle as far as possible to its leftmost ring. But since the data gives the radius, not the coordinates, it is to express the coordinates of the previously arranged circle.

Each circle's ordinate is fixed is its radius, the horizontal axis to calculate the use of the circle in front of the horizontal axis, and then the current circle and its front of the circle of the center of the distance.


Get_dis (C1,C2) in the code indicates the horizontal distance between the C1 circle and the C2 round. X[] Array records the horizontal axis of the circle. Consider how the first circle is selected, tangent to all the previously placed rings, and then record all the horizontal distances to find the largest horizontal axis, which is the position where the I circle should be placed. As shown in figure

The black circle is placed before, the first I circle and the front of the 3 black Circle tangent processing, find the most right, that is, the largest of the horizontal axis is the position should be placed (as shown in the red circle is)

This problem can also be pruned processing, of course, at this time to use deep search, if placed some of the ring has not been placed to find the solution than the previous found to be larger, you can prune.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.