11853 - Paintball (UVA)

题目链接如下:

Online Judge

这道题挺可惜,我思路其实就差了一点点没想出来,还是看了uva 11853 paintball(好题)——yhx_yhx. live-CSDN博客 这里的文字部分才最终写出来。

dfs版本代码如下:

#include <cstdio>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
const int maxx = 1001;
const double eps = 1e-7;
// #define debug

struct oppo{
    double x, y, radius;
};
int n;
bool vis[maxx];
double x, y, radius, yLeft, yRight;
std::vector<oppo> vec;
std::set<int> up;

bool intersect(int a, int b){
    double dis1 = (vec[a].radius + vec[b].radius) * (vec[a].radius + vec[b].radius);
    double dis2 = (vec[a].x - vec[b].x) * (vec[a].x - vec[b].x) + (vec[a].y - vec[b].y) * (vec[a].y - vec[b].y);
    if (dis1 > dis2 - eps){
        return true;
    }
    return false;
}

bool dfs(int k){
    vis[k] = true;
    if (vec[k].y < vec[k].radius + eps){
        return false;
    }
    if (vec[k].x < vec[k].radius + eps){
        yLeft = std::min(yLeft, vec[k].y - sqrt(vec[k].radius * vec[k].radius - vec[k].x * vec[k].x));
    }
    if (vec[k].x > 1000 - vec[k].radius - eps){
        yRight = std::min(yRight, vec[k].y - sqrt(vec[k].radius * vec[k].radius - (1000 - vec[k].x) * (1000 - vec[k].x)));
    }
    for (int i = 0; i < n; ++i){
        if (i != k && !vis[i] && intersect(i, k)){
            if (!dfs(i)){
                return false;
            }
        }
    }
    return true;
}

void judge(){
    yLeft = yRight = 1000.0;
    for (auto it = up.begin(); it != up.end(); ++it){
        std::fill(vis, vis + n, false);
        if (!dfs(*it)){
            printf("IMPOSSIBLE\n");
            return;
        }
    }
    printf("0.00 %.2f 1000.00 %.2f\n", yLeft, yRight);
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d", &n) == 1){
        vec.resize(n);
        up.clear();
        for (int i = 0; i < n; ++i){
            scanf("%lf %lf %lf", &x, &y, &radius);
            vec[i].x = x;
            vec[i].y = y;
            vec[i].radius = radius;
            if (y > 1000 - radius - eps){
                up.insert(i);
            }
        }
        judge();
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

我自己最开始的想法是用连通集来做,也能AC:

#include <cstdio>
#include <vector>
#include <set>
#include <cmath>
const int maxx = 1001;
const double eps = 1e-7;
// #define debug

struct oppo{
    double x, y, radius;
};
int n, temp;
int fa[maxx];
double x, y, radius, yLeft, yRight;
std::vector<oppo> vec;
std::set<int> up, down, left, right;

bool intersect(int a, int b){
    double dis1 = (vec[a].radius + vec[b].radius) * (vec[a].radius + vec[b].radius);
    double dis2 = (vec[a].x - vec[b].x) * (vec[a].x - vec[b].x) + (vec[a].y - vec[b].y) * (vec[a].y - vec[b].y);
    if (dis1 > dis2 - eps){
        return true;
    }
    return false;
}

int findFather(int a){
    while (fa[a] != a){
        a = fa[a];
    }
    return a;
}

void Union(int a, int b){
    int faA = findFather(a);
    int faB = findFather(b);
    fa[faA] = faB;
}

bool divide(){
    std::set<int> top;
    for (auto it = up.begin(); it != up.end(); ++it){
        top.insert(fa[*it]);
    }
    for (auto it = down.begin(); it != down.end(); ++it){
        if (top.find(fa[*it]) != top.end()){
            return true;
        }
    }
    return false;
}

void findPath(){
    yLeft = yRight = 1000.0;
    for (auto it = up.begin(); it != up.end(); ++it){
        temp = fa[*it];
        for (auto it1 = left.begin(); it1 != left.end(); ++it1){
            if (fa[*it1] == temp){
                yLeft = std::min(yLeft, vec[*it1].y - sqrt(vec[*it1].radius * vec[*it1].radius - vec[*it1].x * vec[*it1].x));
            }
        }
        for (auto it2 = right.begin(); it2 != right.end(); ++it2){
            if (fa[*it2] == temp){
                yRight = std::min(yRight, vec[*it2].y - sqrt(vec[*it2].radius * vec[*it2].radius - (1000 - vec[*it2].x) * (1000 - vec[*it2].x)));
            }
        }
    }
    printf("0.00 %.2f 1000.00 %.2f\n", yLeft, yRight);
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d", &n) == 1){
        vec.resize(n);
        up.clear();
        down.clear();
        left.clear();
        right.clear();
        for (int i = 0; i < n; ++i){
            fa[i] = i;
        }
        for (int i = 0; i < n; ++i){
            scanf("%lf %lf %lf", &x, &y, &radius);
            vec[i].x = x;
            vec[i].y = y;
            vec[i].radius = radius;
            if (x < radius + eps){
                left.insert(i);
            }
            if (x > 1000 - radius - eps){
                right.insert(i);
            }
            if (y > 1000 - radius - eps){
                up.insert(i);
            }
            if (y < radius + eps){
                down.insert(i);
            }
            for (int j = 0; j < i; ++j){
                if (intersect(i, j)){
                    Union(i, j);
                }
            }
        }
        for (int i = 0; i < n; ++i){
            fa[i] = findFather(i);
        }
        if (divide()){
            printf("IMPOSSIBLE\n");
        } else {
            findPath();
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值