11853 - Paintball (UVA)

题目链接如下:

Online Judge

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

dfs版本代码如下:

cpp 复制代码
#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:

cpp 复制代码
#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;
}
相关推荐
驱动探索者15 分钟前
linux mailbox 学习
linux·学习·算法
ringking12318 分钟前
autoware-1:安装环境cuda/cudnn/tensorRT库函数的判断
人工智能·算法·机器学习
大闲在人1 小时前
8. 供应链与制造过程术语:产能
算法·制造·供应链管理·智能制造·工业工程
一只小小的芙厨1 小时前
寒假集训笔记·以点为对象的树形DP
c++·算法
历程里程碑1 小时前
普通数组----合并区间
java·数据结构·python·算法·leetcode·职场和发展·tornado
执风挽^1 小时前
Python基础编程题2
开发语言·python·算法·visual studio code
Z9fish1 小时前
sse哈工大C语言编程练习20
c语言·开发语言·算法
晓13132 小时前
第六章 【C语言篇:结构体&位运算】 结构体、位运算全面解析
c语言·算法
iAkuya2 小时前
(leetcode)力扣100 61分割回文串(回溯,动归)
算法·leetcode·职场和发展
梵刹古音2 小时前
【C语言】 指针与数据结构操作
c语言·数据结构·算法