Paintball
You are playing paintball on a 1000 × 1000 square field. A
number of your opponents are on the field hiding behind trees
at various positions. Each opponent can fire a paintball a
certain distance in any direction. Can you cross the field
without being hit by a paintball?
Assume that the southwest corner of the field is at (0, 0)
and the northwest corner at (0,1000).
Input
The input contains several scenario. Each scenario consists
of a line containing n ≤ 1000, the number of opponents. A
line follows for each opponent, containing three real numbers:
the (x, y) location of the opponent and its firing range. The
opponent can hit you with a paintball if you ever pass within
his firing range.
You must enter the field somewhere between the southwest
and northwest corner and must leave somewhere between the
southeast and northeast corners.
Output
For each scenario, if you can complete the trip, output four real numbers with two digits after the
decimal place, the coordinates at which you may enter and leave the field, separated by spaces. If you
can enter and leave at several places, give the most northerly. If there is no such pair of positions, print
the line:‘IMPOSSIBLE’
一道思维特别巧妙的题目;
1.我们要先判断是否有解,我们可以从以上边界相交或相切的圆中开始遍历,找出每个相连的圆,如果找到一个圆与下边界相交或相切,那么无解;
2.在找的过程中,我们同时找出跟左边界相切或相交的圆,因为和上边界相切和相交的圆连在一起了,所以这个圆与左边界相交的点以上的点必定是死胡同,没有出口;右边界也是一样;
3.千万记得进出点初始化为1000;
代码:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=1000100;
const LL mod=998244353;
struct Node{
double x,y,z;
}opp[1100];
int ok,flag;
int n;
double in,out;
bool vis[1100];
vector<int>ve;
double len(int p,int q){
return sqrt((opp[p].x-opp[q].x)*(opp[p].x-opp[q].x)+(opp[p].y-opp[q].y)*(opp[p].y-opp[q].y));
}
void dfs(int p){
if(ok) return;
if(opp[p].x<=opp[p].z){//与左边界相交
double d=opp[p].y-sqrt(opp[p].z*opp[p].z-opp[p].x*opp[p].x);
in=min(in,d);
}
if(1000.0-opp[p].x<=opp[p].z){//与右边界相交
double r=1000.0-opp[p].x;
double d=opp[p].y-sqrt(opp[p].z*opp[p].z-r*r);
out=min(out,d);
}
if(opp[p].y-opp[p].z<=0){
ok=1;
return;
}
for(int i=1;i<=n;i++){
if(vis[i]) continue;
if(len(p,i)<=opp[p].z+opp[i].z){
vis[i]=true;
dfs(i);
}
}
}
void Init(){
in=out=1000;
ve.clear();
flag=0;
memset(vis,false,sizeof(vis));
}
int main(){
// ios::sync_with_stdio(false);
while(cin>>n){
Init();
for(int i=1;i<=n;i++){
cin>>opp[i].x>>opp[i].y>>opp[i].z;
if(opp[i].y+opp[i].z>=1000) ve.push_back(i);
}
for(int i=0;i<ve.size();i++){
if(vis[ve[i]]) continue;
ok=0;
dfs(ve[i]);
if(ok==1) flag=1;
}
if(flag==0) printf("0.00 %.2lf 1000.00 %.2lf\n",in,out);
else cout<<"IMPOSSIBLE"<<endl;
}
return 0;
}