Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

圆排列问题

$
0
0
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std ;
 
class Circles{
public :
    int n ;
    float min ;
    float x[50];
    float r[50];
 
    float circlePerm(int nn ,  float rr[50])
    {
        n =nn ;
        min = 10000000 ;
        memset(x,0,sizeof(x));
        copy(rr,rr+50,r);
        backtrack(1);
        return min ;
    }
    void backtrack( int t)
    {
        if(t > n )
            compute();
        else {
            for (int j =t ; j<= n ; j++ )
            {
                swap(r[t],r[j]);
                float centerx = center(t);
                if(centerx+r[t]+r[1] < min ){//below bound
                    x[t] = centerx ;
                    backtrack(t+1);
                }
                swap(r[t],r[j]);
            }
        }
    }
    float center(int t )
    {
        float temp = 0 ;
        for ( int j =1 ; j<t ; j++ )
        {
            float valuex =(float)(x[j]+2.0*sqrt(r[t]*r[j]));
            if(valuex>temp) temp = valuex;
        }
        return temp;
    }
     void compute()
    {
        float low = 0 ;
        float high = 0 ;
        for ( int i = 1 ;i <=n ;i++)
        {
            if(x[i]-r[i]<low)
                low = x[i] - r[i];
            if(x[i]+r[i]>high)
                high = x[i] + r[i];
        }
        if(high - low < min )
            min = high - low ;
    }
};
 
int main()
{
 
    int nn ;
    int TT ;
    int j =1 ;
    float rr[50];
    Circles cs ;
    cin>>TT;
    while(TT>0)
    {
        cin>>nn ;
         
        int i = 1 ;
        while(i<=nn)
        {
            cin>>rr[i];
            i++;
        }
        cs.circlePerm( nn ,  rr);
        printf("Case %d: %0.2f\n",j,cs.min);
        j++;
        TT--;
    }
    return 0 ;
}
作者:laohan_ 发表于2012-12-31 23:57:15 原文链接
阅读:51 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>