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;
}
相关推荐
挺菜的30 分钟前
【算法刷题记录(简单题)002】字符串字符匹配(java代码实现)
java·开发语言·算法
凌肖战4 小时前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
88号技师5 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
ゞ 正在缓冲99%…5 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
Kaltistss6 小时前
98.验证二叉搜索树
算法·leetcode·职场和发展
知己如祭6 小时前
图论基础(DFS、BFS、拓扑排序)
算法
mit6.8246 小时前
[Cyclone] 哈希算法 | SIMD优化哈希计算 | 大数运算 (Int类)
算法·哈希算法
c++bug6 小时前
动态规划VS记忆化搜索(2)
算法·动态规划
哪 吒6 小时前
2025B卷 - 华为OD机试七日集训第5期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
python·算法·华为od·华为od机试·2025b卷
军训猫猫头7 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net