uva 10012 How Big Is It?

点击打开链接 (这个链接到BNUOJ上面是一样的)

题目意思:  给定m个圆的半径,现在要求找到一个矩形使得每一个球都以地面相切,要求输出最小的矩阵的长度

解题思路:  最小的圆排列问题,对于这样的一个图形我么是转化为坐标来计算,那么我们需要做的三个步骤就是  1  递归去放入每一个圆  2 求出每一个圆的圆心的横坐标  3计算最小的长度 。 具体过程见代码分析:

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

int n , m;
double r[10];//用来存储每一个圆的半径
double p[10];//存储圆的横坐标
double ans;

//求解第k个圆的横坐标函数(也就是说,第k个圆很可能跟前面任何一个圆相切。这时,只要把各种情况得到取值全部算出,并把最大值 记录下来)
//我们知道对于从左往右的所有的圆来说,圆心的横坐标都是增大,不用考虑圆的大小,那么对于k这个圆,它是最右边的那么我们找到最大的横坐标才是它的横坐标值
double Point(int k){
    double R = 0;
    for(int j = 1 ; j < k ; j++){
        double tempr = p[j] + 2.0*sqrt(r[k]*r[j]);//两个圆的距离 d = sqrt((r1+r2)^2 - (r1-r2)^2) 那么有 d  =  2.0*sqrt(r[k]*r[j]);
        if(R < tempr)//更新为最大
            R = tempr;
    }
    return R;
}

//计算当前的圆排列的长度
//p[i]-r[i],就是该圆最左边的切线的横坐标   p[i]+r[i]就是该圆最右边的切线的横坐标,那么对于这个圆排列的长度就是最右边的横坐标减去最左边的横坐标
void Distance(){
    double high , low;
    high = 0 ; low = 0;
    for(int i = 1 ; i <= m ; i++){
        if(p[i]-r[i] < low)  low  = p[i] - r[i];     //更新最左边的位置
        if(p[i]+r[i] > high) high = p[i] + r[i]; //更新最右边的位置
    }
    if(high - low < ans)
        ans = high - low;//更新最小距离
}

//回溯搜索函数
void dfs(int k){
    if(k > m)//圆放完后就是计算这个排列的长度
        Distance();
    else{
        for(int j = k ; j <= m ; j++){
            swap(r[k] , r[j]);//考虑放入k后,序列长度; 还要考虑继第k个球之后,余下的m-k个球放入后,对整个序列的长度的影响.
            double R = Point(k);
            if(R+r[k]+r[1] < ans){//满足条件继续递归
                p[k] = R;
                dfs(k+1);
            }
            swap(r[k] , r[j]);//现场的恢复
        }
    }
}

//主函数
int main(){
    scanf("%d" , &n);
    while(n--){
        scanf("%d" , &m);
        memset(r , 0.0 , sizeof(r));
        memset(p , 0.0 , sizeof(p));
        for(int i = 1 ; i <= m ; i++)
            scanf("%lf" , &r[i]);
        p[1] = 0.0;//规定第一个点的横坐标为0
        ans = 999999999;
        dfs(1);
        printf("%.3lf\n" , ans);//输出的格式
    }
    return 0;
}
时间: 2024-07-29 06:03:51

uva 10012 How Big Is It?的相关文章

UVa 10012 How Big Is It? (枚举和细节)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=953 思路: 最多8个球,直接枚举. 注意: 1. 由于一个圆不一定与圆心横坐标与其横坐标最接近的圆相切,所以判断一个圆圆心横坐标的实际位置要一一比较在它左边的每个圆,取最大值. 2. 由于圆心横坐标在最右边的圆不一定与箱子最右端接触(如上图,我们的算法是保证每

UVa 10012:How Big Is It? 圆排列问题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=953 类型: 回溯 原题: Ian's going to California, and he has to pack his things, including his collection of circles. Given a set of

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i