码迷,mamicode.com
首页 > 其他好文 > 详细

[dfs]11853 - Paintball

时间:2015-06-26 00:25:06      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

You are playing paintball on a 1000 1000 square eld. A
number of your opponents are on the eld hiding behind trees
at various positions. Each opponent can re a paintball a
certain distance in any direction. Can you cross the eld
without being hit by a paintball?
Assume that the southwest corner of the eld 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 ring range. The
opponent can hit you with a paintball if you ever pass within
his ring range.
You must enter the eld 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 eld, 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<cstdio>
#include<cstring>
//#include<vector>
//#include<queue>
#include<algorithm>
#include<math.h>
//#define local
using namespace std;

const int maxn = 1000 + 1;
const double W = 1000;

int x[maxn], y[maxn], r[maxn];
int n;
double left,right;

bool intersect(int a,int b) {
   return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b])) < r[a]+r[b];
}
void check(int u)
{
   if(x[u] < r[u]) left = min(left, y[u] - sqrt(r[u]*r[u] - x[u]*x[u]));
   if(x[u]+r[u] > W) right = min(right,y[u] - sqrt(r[u]*r[u] - (W-x[u])*(W-x[u]) ) ) ;
}
int vis[maxn];
//top to bottom
bool dfs(int u)
{
   if(vis[u]) return false;
   vis[u] = 1;
   if(y[u] < r[u]) return true;
   for(int v = 0; v < n; v++){
      if(intersect(u,v) && dfs(v)) return true;
   }
   check(u);
   return false;
}


int main()
{
#ifdef local
    freopen("in.txt","r",stdin);
#endif // local
   while(~scanf("%d",&n)) {
      bool ok = true;
      memset(vis,0,sizeof(vis));
      left = right = W;
      for(int i = 0; i < n; i ++){
         scanf("%d%d%d",x+i,y+i,r+i);
      }
      for(int i = 0; i < n; i ++) {
         if(r[i]+y[i]>=W && dfs(i)) {ok = false; break;}
      }
      if(ok) printf("0.00 %.2lf 1000.00 %.2lf\n",left,right);
      else printf("IMPOSSIBLE\n");
   }
   return 0;
}

 

[dfs]11853 - Paintball

标签:

原文地址:http://www.cnblogs.com/jerryRey/p/4601283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!