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;
}
相关推荐
熊猫_豆豆2 小时前
基于改进沙猫群优化算法的Otsu图像分割
人工智能·算法·计算机视觉
吃着火锅x唱着歌2 小时前
LeetCode 624.数组列表中的最大距离
数据结构·算法·leetcode
im_AMBER2 小时前
Leetcode 64 大小为 K 且平均值大于等于阈值的子数组数目
笔记·学习·算法·leetcode
AndrewHZ2 小时前
【图像处理基石】什么是图像处理中的注意力机制?
图像处理·pytorch·深度学习·算法·计算机视觉·注意力机制·通道注意力
DuHz2 小时前
通感一体化(ISAC)波形设计的实验验证研究——论文阅读
论文阅读·算法·信息与通信·毫米波雷达
CoderYanger2 小时前
递归、搜索与回溯-综合练习:22.优美的排列
java·算法·leetcode·深度优先·1024程序员节
慕沐.2 小时前
【算法】冒泡排序的原理及实现
java·算法·排序算法
TracyCoder1232 小时前
分布式算法(八):一致性哈希——分布式系统的负载均衡利器
分布式·算法·哈希算法
Juan_20122 小时前
P2865 [USACO06NOV] Roadblocks G 题解
c++·算法·图论·题解
MediaTea2 小时前
Python 库手册:gc 垃圾回收
java·开发语言·jvm·python·算法