0
点赞
收藏
分享

微信扫一扫

UVA How Big Is It? (DFS+回溯)

皮皮球场 2023-04-21 阅读 35


  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 integer 

m ,

m <=8, which indicates how many other numbers appear on that line. The next 

m  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.0 4 2.0 2.0 2.0 2.0 3 2.0 1.0 4.0


Sample Output 


9.65716.000 12.657


Miguel Revilla
2000-08-22




    题意:给出n个圆,求满足这n个圆全部都可以着地的情况下两侧最小的长度。

因为这题需要考虑的情况很多,当时有的情况没有考虑,又是几个WA白白贡献了。

可以参考一下这篇博客,他把所有的情况全都画出来了,比较形象。


代码:

DFS+回溯;



#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

using namespace std;

double r[10],R[10];
int n;
int v[10];
double a[10];
double maxx;

void DFS(int m)
{
    if(m == n)
    {
        a[0] = r[0];
        for(int i=1; i<n; i++)
        {
            a[i] = 0;
            for(int j=0; j<i; j++)
            {
                double l = sqrt((r[i]+r[j])*(r[i]+r[j]) - (r[i]-r[j])*(r[i]-r[j])) + a[j];
                if(l>a[i])
                {
                    a[i] = l;
                }
            }
        }
        double left,right;
        left = 999999999;
        right = -99999999;
        for(int i=0; i<n; i++)
        {
            if(a[i]+r[i]>right)
            {
                right = a[i]+r[i];
            }
            if(a[i]-r[i]<left)
            {
                left = a[i] - r[i];
            }
        }
        if(right - left < maxx)
        {
            maxx = right - left;
        }
        return ;
    }
    for(int i=0; i<n; i++)
    {
        if(v[i] == 0)
        {
            v[i] = 1;
            r[m] = R[i];
            DFS(m+1);
            v[i] = 0;
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%lf",&R[i]);
        }
        memset(v,0,sizeof(v));
        maxx = 99999999;
        DFS(0);
        printf("%.3lf\n",maxx);
    }
    return 0;
}




DFS + 全排列:




#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

using namespace std;

double r[10],R[10];
int n;
int v[10];
double a[10];
double maxx;

void DFS()
{
    a[0] = r[0];
    for(int i=1; i<n; i++)
    {
        a[i] = 0;
        for(int j=0; j<i; j++)
        {
            double l = sqrt((r[i]+r[j])*(r[i]+r[j]) - (r[i]-r[j])*(r[i]-r[j])) + a[j];
            if(l>a[i])
            {
                a[i] = l;
            }
        }
    }
    double left,right;
    left = 999999999;
    right = -99999999;
    for(int i=0; i<n; i++)
    {
        if(a[i]+r[i]>right)
        {
            right = a[i]+r[i];
        }
        if(a[i]-r[i]<left)
        {
            left = a[i] - r[i];
        }
    }
    if(right - left < maxx)
    {
        maxx = right - left;
    }

}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%lf",&r[i]);
        }
        maxx = 99999999;
        sort(r,r+n);
        do
        {
            DFS();
        }while(next_permutation(r,r+n));
        printf("%.3lf\n",maxx);
    }
    return 0;
}





举报

相关推荐

0 条评论