UVA10012 is not so simple, there is a trap!

Source: Internet
Author: User
This problem is the first time to do it as a general retrospective, the original idea is to arrange the whole circle, place a new circle with the previous circle tangent, and finally by backtracking to get the smallest size of the rectangle. More than 10 minutes after the end of the result WA, think for a while and find out what the problem is, you can't just make it tangent to the previous circle (if the first circle is large, the radius is 100, the second circle is small, the radius is 1, the third circle is large, the radius is 100, When you put the third circle, if you are tangent to the second circle, you will not be able to intersect the first circle. Then you start to modify the code, and then you get all sorts of bugs, and you've been on and off all night and all morning.   The first thing to think about is that each time you put a circle or you let it tangent to the previous place, and then determine whether it intersects with the previous circle, such as disjoint, then it is correct and the most economical distance, if any one of the circle and its intersection, then the previous circle can not be tangent, then push one forward, Let it tangent to the previous circle, and then judge whether to intersect with other circles ... To determine whether to intersect, a center array is added to store the center position of each circle. The idea was correct and clear, and then encountered three WA points.   WA1: When you judge the smallest size of a rectangle, you cannot use the rightmost position of the last circle. Because it is possible that the last round is very small, its rightmost position is not as far as the rightmost position of the previous circle, so use the maximum value of the radius of the center of the circle as the smallest size of the rectangle instead.   WA2: When placing the first few rounds of attention can not let the circle center position-the radius of the circle <0. For example, the first circle radius is 1, the second element is 100, and the second element cannot be cut to the first round because the left side of the circle is beyond the wall of the rectangular box!   WA3: The main function of the MINL set is too small, the number of estimated test data is very large, the title does not say the maximum radius is how much, began to set is 65536, the result WA, changed to Dbl_max on AC!!    
#include <iostream> #include <cmath> #include <iomanip> #include <cstring> #include <float.  
  
H> using namespace std;          int m,put[10];  
  
Put[i]: Place the first round of the number double Minl,size[10],center[10];//size[i]: The radius of the circle numbered I; Center[i]: The position of the center of the first circle of the place bool vis[10]; Double Getlen (int a,int b)//Calculate the distance between the two centers {return sqrt ((size[a]+size[b)) * (Size[a]+size[b])-(SIZE[A]-SIZE[B)) * (s  
IZE[A]-SIZE[B])); Whether the bool isOK (int a,int b)//is intersected with a previously placed circle {for (int i=0;i<b;i++) {if (sqrt (center[i]- Center[a]) * (Center[i]-center[a]) + (Size[put[i]]-size[put[a]]) (Size[put[i]]-size[put[a])) < (size[put[i]]+  
    Size[put[a]]) return false;  
return true;  
    } void Dfs (int cur) {int i,j;  
        if (cur==m) {double maxsize=0; for (int k=0;k<cur;k++) {if (center[k]+size[put[k]]>maxsize) maxsize=cent  
       Er[k]+size[put[k]]; } if (MAXSIZE&LT;MINL) minl=maxsize;  
                else {for (i=0;i<m;i++) {if (!vis[i]) {  
                Put[cur]=i;  
                Vis[i]=1;  
                Double Tmpl;  
                BOOL Ok=1;  
                    for (j=cur-1;j>=0;j--) {Tmpl=getlen (put[j],i);  
                    Center[cur]=center[j]+tmpl;  
                    if (center[cur]-size[put[cur]]<0) {ok=0;break;}  
                if (isOK (CUR,J)) break;  
                } if (OK) DFS (cur+1);  
            vis[i]=0;  
    int main () {int n;  
    cin>>n;  
        while (n--) {cin>>m;  
        Minl=dbl_max;  
        memset (Vis) (vis,0,sizeof);  
        for (int i=0;i<m;i++) cin>>size[i]; for (int i=0;i<m;i++) {put[0]=i;  
            Center[0]=size[i];  
            Vis[i]=1;  
            DFS (1);  
        vis[i]=0;  
    } cout<<fixed<<setprecision (3) <<MinL<<endl;  
return 0;  
 }


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.