同swustoj 8

Snakes
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1015   Accepted: 341

Description

Buffalo Bill wishes to cross a 1000x1000 square field. A number of snakes are on the field at various positions, and each snake can strike a particular distance in any direction. Can Bill make the trip without being bitten?

Input

Assume that the southwest corner of the field is at (0,0) and the northwest corner at (0,1000). The input consists of a line containing n <= 1000, the number of snakes. A line follows for each snake, containing three real numbers: the (x,y) location of the snake and its strike distance. The snake will bite anything that passes closer than this distance from its location.

Output

Bill must enter the field somewhere between the southwest and northwest corner and must leave somewhere between the southeast and northeast corners.

If Bill can complete the trip, give coordinates at which he may enter and leave the field. If Bill may enter and leave at several places, give the most northerly. If there is no such pair of positions, print "Bill will be bitten."

Sample Input

3
500 500 499
0 0 999
1000 1000 200

Sample Output

Bill enters at (0.00, 1000.00) and leaves at (1000.00, 800.00).

并查集+计算几何

#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdio>
using namespace std; #define PI acos(-1.0)
#define EPS 1e-8
#define N 1010 int dcmp(double x)
{
if(fabs(x)<EPS) return ;
return x<?-:;
}
struct Point
{
double x,y;
Point (){}
Point (double x,double y):x(x),y(y){}
Point operator - (Point p){
return Point(x-p.x,y-p.y);
}
bool operator == (Point p){
return dcmp(fabs(x-p.x))== && dcmp(fabs(y-p.y))==;
}
double operator * (Point p){
return x*p.x+y*p.y;
}
double operator ^ (Point p){
return x*p.y-y*p.x;
}
double length(){
return sqrt(x*x+y*y);
}
double angle(){
return atan2(y,x);
}
bool operator <(const Point &p)const{
return y<p.y;
}
};
struct Line
{
Point s,e;
Line (){}
Line (Point s,Point e):s(s),e(e){}
Point GetPoint(double t){
return Point(s.x+(e.x-s.x)*t,s.y+(e.y-s.y)*t);
}
};
struct Circle
{
Point c;
double r;
Circle(){}
Circle(Point c,double r):c(c),r(r){}
Point GetPoint(double a){
return Point(c.x+cos(a)*r,c.y+sin(a)*r);
}
/* 0表示相离,1表示相切,2表示相交 */
pair<int,vector<Point> > CircleInterLine(Line l){
vector<Point> res;
double A=l.e.x-l.s.x,B=l.s.x-c.x,C=l.e.y-l.s.y,D=l.s.y-c.y;
double E=A*A+C*C,F=*(A*B+C*D),G=B*B+D*D-r*r;
double delta=F*F-*E*G;
if(dcmp(delta)<) return make_pair(,res);
if(dcmp(delta)==){
res.push_back(l.GetPoint(-F/(*E)));
return make_pair(,res);
}
res.push_back(l.GetPoint((-F-sqrt(delta))/(*E)));
res.push_back(l.GetPoint((-F+sqrt(delta))/(*E)));
return make_pair(,res);
}
/* -1表示重合,0表示相离,1表示相切,2表示相交 */
int operator & (Circle C){
double d=(c-C.c).length();
if(dcmp(d)==){
if(dcmp(r-C.r)==) return -;
return ;
}
if(dcmp(r+C.r-d)<) return ;
if(dcmp(fabs(r-C.r)-d)>) return ;
double a=(C.c-c).angle();
double da=acos((r*r+d*d-C.r*C.r)/(*r*d));
Point p1=GetPoint(a-da),p2=GetPoint(a+da);
if(p1==p2) return ;
return ;
}
}; int n;
int f[N];
Line up,down;
Line lft,rgt;
Circle c[N]; void init()
{
for(int i=;i<=n+;i++) f[i]=i;
up=Line(Point(,),Point(,));
down=Line(Point(,),Point(,));
lft=Line(Point(,),Point(,));
rgt=Line(Point(,),Point(,));
}
int Find(int x)
{
if(x!=f[x]) f[x]=Find(f[x]);
return f[x];
}
void UN(int x,int y)
{
x=Find(x);
y=Find(y);
if(x!=y) f[x]=y;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=;i<=n;i++) scanf("%lf%lf%lf",&c[i].c.x,&c[i].c.y,&c[i].r);
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if((c[i]&c[j])!=){
UN(i,j);
}
}
}
//上边界
for(int i=;i<=n;i++){
pair<int,vector<Point> > res=c[i].CircleInterLine(up);
if(res.first!=) UN(,i);
}
//下边界
for(int i=;i<=n;i++){
pair<int,vector<Point> > res=c[i].CircleInterLine(down);
if(res.first!=) UN(i,n+);
}
if(Find()==Find(n+)){ //出不去
printf("Bill will be bitten.\n");
continue;
}
//左右边界
vector<Point> p1,p2;
p1.push_back(Point(,));
p2.push_back(Point(,));
for(int i=;i<=n;i++){
pair<int,vector<Point> > res1=c[i].CircleInterLine(lft);
pair<int,vector<Point> > res2=c[i].CircleInterLine(rgt);
if(res1.first!=){
while(!res1.second.empty()){
if(res1.second.back().y>= && res1.second.back().y<= && Find(i)==Find())
p1.push_back(res1.second.back());
res1.second.pop_back();
}
}
if(res2.first!=){
while(!res2.second.empty()){
if(res2.second.back().y>= && res2.second.back().y<= && Find(i)==Find())
p2.push_back(res2.second.back());
res2.second.pop_back();
}
}
}
int i,j;
sort(p1.begin(),p1.end());
sort(p2.begin(),p2.end());
printf("Bill enters at (0.00, %.2f) and leaves at (1000.00, %.2f).\n",p1[].y,p2[].y);
}
return ;
}

[POJ 2588] Snakes的更多相关文章

  1. [POJ 2588]--Snakes(并查集)

    题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS   Memory Limit: 65536K   Description B ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

随机推荐

  1. makefile:4: *** missing separator. Stop.

    今天在编写蜂鸣器的驱动程序时,makefile文件是这样: CROSS=arm-linux- all: beep beep: beep.c $(CROSS)gcc -o beep beep.c $(C ...

  2. Spark Streaming揭秘 Day32 WAL框架及实现

    Spark Streaming揭秘 Day32 WAL框架及实现 今天会聚焦于SparkStreaming中非常重要的数据安全机制WAL(预写日志). 设计要点 从本质点说,WAL框架是一个存储系统, ...

  3. 在MVVMLight框架的ViewModel中实现NavigationService

    网上已经有很多方法了,比如通过Messenger来实现等等.这里我只讲述一种我比较喜欢的方法,因为它很方便 首先定义一个ViewModel基类,将所有ViewModel子类继承这个基类.在基类中定义 ...

  4. 【git】学习路径失败了

    期初规划:搭建git远程服务器  使用gitlab作为管理工具 过程遇到的问题 1.gitlab不能安装到win ,且对centos要求6以上,我只有一台centos5  让运维帮升级 ...等待.. ...

  5. 配置spring的事务管理

    网上看到过很多关于spring事务管理的东西,但是原创的并不多,如果你的运气好能看到那些原创的文章恭喜你,因为我看到的不多,但一些原创的文 章里面枝叶太多,因为那些高手直接把自己的代码拷过来,所以说无 ...

  6. maven3.1安装及配置

    1.首先下载maven3,并安装 2.环境变量配置与JAVA_HOME的配置一样 3.打开MyEclipse preferences>>Maven4MyEclipse>>Mav ...

  7. TF/IDF计算方法

    FROM:http://blog.csdn.net/pennyliang/article/details/1231028 我们已经谈过了如何自动下载网页.如何建立索引.如何衡量网页的质量(Page R ...

  8. C# Windows - RadioButton&CheckBox

    RadioButton和CheckBox控件与Button控件有相同的基类,但它们的外观和用法大不相同. RadioButton显示为一个标签,左边是一个圆点,该点可以是选中或未选中.用在给用户提供两 ...

  9. C#学习笔记---基础入门(三)

    泛型<T> 使用泛型能够最大限度的重用代码/保护类型安全,提高性能 泛型成员因为类型的不确定性,不能使用算术运算符/比较运算符 类型参数可以有多个,可以是编译器能够识别的任何类型 类型参数 ...

  10. python 读写 Excel文件

    最近用python处理一个小项目,其中涉及到对excel的读写操作,通过查资料及实践做了一下总结,以便以后用. python读写excel文件要用到两个库:xlrd和xlwt,首先下载安装这两个库. ...