题目链接如下:
这道题挺可惜,我思路其实就差了一点点没想出来,还是看了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;
}