[POJ 2588] Snakes
同swustoj 8
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1015 | Accepted: 341 |
Description
Input
Output
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的更多相关文章
- [POJ 2588]--Snakes(并查集)
题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS Memory Limit: 65536K Description B ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- 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 ...
随机推荐
- Python在Windows下开发环境配置汇总
最近比较关注学习Python方面的资料和课程,由于Python本身基本都是在Linux下开发,本人windows用习惯了初用Linux各种别扭啊. 下面将我在配置Windows环境下的禁言写出来,与大 ...
- Beaglebone Back学习四(GPIO实验)
GPIO Beaglebone Back开发板引出了92个引脚,其中只有65个GPIO口可通过配置使用,由于引脚具有“复用”的特性,大约每个引脚有8种工作模式(Beagle System Refere ...
- cc2640-各DEMO板性能分析
一.测试方法: 将4种模块同时上电,测量每个模块达到的最远距离.以稳定能建立通讯为连接上依据.4种板子分析为 1号阿莫DEMO板,2号咱们自己DEMO板,3号嘉源电子DEMO,4号陆程电子DEMO 全 ...
- The 50 Most Essential Pieces of Classical Music
1. Die Zauberflöte ("The Magic Flute"), K. 620: Overture London Philharmonic Orchestra 7:2 ...
- go语言使用protobuf
网上为什么充斥着大量几乎一模一样而且不正确的教程??? 妈的打开一个关于golang和protobuf的教程,无非都是protobuf多么多么牛逼,xml多么多么傻逼,然后就是怎么安装protobuf ...
- 配置spring的事务管理
网上看到过很多关于spring事务管理的东西,但是原创的并不多,如果你的运气好能看到那些原创的文章恭喜你,因为我看到的不多,但一些原创的文 章里面枝叶太多,因为那些高手直接把自己的代码拷过来,所以说无 ...
- Qt的gzip模块实现
一直没找到Qt中方便的gzip模块,于是自己动手,调用zlib模块实现了一份. 目标: 1.gzip的压缩与解压 2.内存中操作 3.方便的Qt接口 实现分析: gzip 压缩算法为 defla ...
- Nhibernate 多对多级联删除
在网上找到的方法:查看这里 //-------------------------------------Article.hbm.xml-------------------------------- ...
- Extjs4.2布局——layout: accordion(Ext.layout.container.Accordion)
API这样介绍这种布局: 示例:(来自API)注:打开默认展开第一个面板. Ext.create('Ext.panel.Panel', { title: 'Accordion Layout', wid ...
- TIANKENG’s restaurant
Problem B:http://codeforces.com/contest/616/problem/B B. Dinner with Emma 题意:一对夫妻要去餐厅吃晚饭,Emma 想去最豪华( ...