aoapc第六章 V题 Paintball
http://7xjob4.com1.z0.glb.clouddn.com/ba37417d3e441b0072750dba18f2676d
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’
Sample Input
3
500 500 499
0 0 999
1000 1000 200
Sample Output
0.00 1000.00 1000.00 800.00
直接搜空地不好弄,可以反着来搜障碍物。先判断是否有解,再求最优解。
#include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef long long ll; const int maxn=1000100; const int INF=(1<<29); const double EPS=0.00000000001; struct Node { double x,y; double r; void read() { scanf("%lf%lf%lf",&x,&y,&r); } void debug() { printf("x=%.0f y=%.0f r=%.0f ",x,y,r); } };Node p[maxn]; int n; vector<int> up; vector<int> G[maxn]; bool vis[maxn]; double ansL,ansR; bool inter_up(Node p,double y) { return p.y-p.r<=y+EPS&&y<=p.y+p.r+EPS; } bool inter_left(Node p,double x) { return p.x-p.r<=x+EPS&&x<=p.x+p.r+EPS; } double inter_left_p(Node p,double x) { double dt=p.r*p.r-(x-p.x)*(x-p.x); double y1=p.y+sqrt(dt),y2=p.y-sqrt(dt); return min(y1,y2); } int dist2(Node A,Node B) { double tx=A.x-B.x; double ty=A.y-B.y; return tx*tx+ty*ty; } bool inter(Node A,Node B) { double d2=dist2(A,B); double R=A.r+B.r; return R*R>=d2-EPS; } bool dfs(int u) { if(vis[u]) return 1; vis[u]=1; if(inter_up(p[u],0)) return 0; int res=1; for(int i=0;i<G[u].size();i++){ int v=G[u][i]; res&=dfs(v); } return res; } void dfs2(int u) { if(vis[u]) return; vis[u]=1; if(inter_left(p[u],0)){ double tmp=inter_left_p(p[u],0); ansL=min(ansL,tmp); } if(inter_left(p[u],1000)){ double tmp=inter_left_p(p[u],1000); ansR=min(ansR,tmp); } for(int i=0;i<G[u].size();i++){ int v=G[u][i]; dfs2(v); } } int main() { freopen("in.txt","r",stdin); while(cin>>n){ up.clear(); REP(i,0,n) G[i].clear(); REP(i,1,n){ p[i].read(); if(inter_up(p[i],1000)) up.push_back(i); } REP(i,1,n){ REP(j,i+1,n){ if(inter(p[i],p[j])){ G[i].push_back(j); G[j].push_back(i); } } } bool flag=1; MS0(vis); for(int i=0;i<up.size();i++){ if(!dfs(up[i])){ flag=0;break; } } if(!flag){ puts("IMPOSSIBLE");continue; } ansL=ansR=1000; MS0(vis); for(int i=0;i<up.size();i++) dfs2(up[i]); printf("0.00 %.2f 1000.00 %.2f ",ansL,ansR); } return 0; }