[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 ...
随机推荐
- 医学影像工作站程序ProDicom的说明
转载 http://blog.csdn.net/prodicom/article/details/4015064 注意:以下内容为转载,但保留了第一人称,请注意,以免造成不必要的麻烦. 医网联影像工作 ...
- Python问题之奇怪诡异的Bug
最近又重新装上了windows 7感觉还是那样,主要是想用M8SDK写些程序.也想在windows上玩玩,一直都觉得用C写一些常用的东东很复杂,只有借助于解释性语言了,在python, ruby间选择 ...
- 查看某一个点是否在某个多边形内 使用ST_Contains函数
查看某一个点是否在某个多边形内 使用ST_Contains函数 --LINESTRING ( 121.312350 30.971457 , 121.156783 31.092221 , 121.35 ...
- 关于搭建Android环境的时候遇到 'could not find adb.exe!'的问题
关于'could not find adb.exe'的问题 问题原因: 文件所处位置和Android_home变量指路径不一致 文件路径: 解决方法: 直接将相关文件退拽至变量值的路径下即可 小结:a ...
- centos7初步配置
centos7初步配置 首先安装lrzsz zip/unzip yum -y install lrzsz yum -y install zip unzip 安装vim yum install vim* ...
- windows server 2008 r2电脑历史操作记录
1.看计算机哪天运行过. 在系统盘下的Windows\Tasks文件夹下找到文件SCHEDLGU.TXT. 2.看你最近打开过什么文件(非程序)或者文件夹 开始-->运行--> ...
- 1014: [JSOI2008]火星人prefix - BZOJ
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- 2038: [2009国家集训队]小Z的袜子(hose) - BZOJ
Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只 ...
- SQL SERVER数据导入
我的博客已好久没有文字方面的记载了,好歹昨天已经结束软件设计师的考试了,今天怎么说也需要锻炼自己的写作能力.不然真怕自己又像上一年一样,一停就一年多了. 想好好学习数据库(SQL SERVER)方面的 ...
- XML 创建
using unityEngine; using System.Collections; using System.Linq; using System.Xml.Linq; using System; ...