【UVA】10012

使用DFS枚举所有的安排。每次加入后,当一个圆。他的立场是最大的,并已加入了圆环中的所有切线位置前面。

14383635 10012 How Big Is It? Accepted C++ 0.086 2014-10-20 11:07:33

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef double Dou;
#define FOR(i,n) for(int i = 0; i < n; i ++)
const int maxn = 10;
int n,vis[maxn];
Dou ans,r[maxn];
struct POS{   //圆心位置就是(x,r)
    Dou x,r;  //横坐标,半径
}pos[maxn];
Dou check(int cur,int k){
    Dou ret = r[k];
    FOR(i,cur){
        Dou a = pos[i].r + r[k];
        Dou b = pos[i].r - r[k];
        Dou c = sqrt(a * a - b * b);
        ret = max(ret,pos[i].x + c);
    }
    pos[cur].x = ret; pos[cur].r = r[k];
    return ret + r[k];
}
void dfs(int cur,double L){
    if(ans != -1 && L > ans)  return;
    if(cur == n){
        Dou ret;
        for(int i = 0; i < cur; i++){
            if(i == 0) ret = pos[i].x + pos[i].r;
            else ret = max(ret,pos[i].x + pos[i].r);
        }
        ans = (ans == -1) ?

ret : min(ans,ret); return; } FOR(i,n) if(!vis[i]){ vis[i] = 1; dfs(cur + 1,check(cur,i)); vis[i] = 0; } return; } int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d",&n); ans = -1; memset(vis,0,sizeof(vis)); FOR(i,n) scanf("%lf",&r[i]); dfs(0,0.0); printf("%.3f ",ans); } return 0; }


原文地址:https://www.cnblogs.com/blfshiye/p/5038515.html