[POJ 2588]--Snakes(并查集)
题目链接:http://poj.org/problem?id=2588
Snakes
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 Sample Output Bill enters at (0.00, 1000.00) and leaves at (1000.00, 800.00). Source |
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 1005 /*-------------上下左右边界判断-------------*/
struct UpDown{
double x, y, area;
}snack[maxn];
struct LeftRight{
double up, down;
int i;
}le[maxn], ri[maxn]; int father[maxn], map[maxn][maxn]; //----------检查当前出入最高点是否可用--------------------
bool check(LeftRight *p, double x, int n){
for (int i = ; i < n; i++){
if (p[i].up>x&&p[i].down < x)
return false;
}
return true;
} //-------------并查集判断构造的点(包括上下边界)是否连通-------------------
int getf(int x){
return father[x] != x ? father[x] = getf(father[x]) : x;
} int main()
{
int n, i, j;
while (cin >> n){
for (i = ; i <= n + ; i++)
father[i] = i;
int L = , R = ;
memset(map, , sizeof(map));
for (i = ; i <= n; i++){
cin >> snack[i].x >> snack[i].y >> snack[i].area; //-----------建立上下关系---------------
if (snack[i].y + snack[i].area > )
map[][i] = map[i][] = ;
if (snack[i].y - snack[i].area < )
map[n + ][i] = map[i][n + ] = ; //---------建立左右关系------------------
if (snack[i].x - snack[i].area < ){
le[L].up = snack[i].y + sqrt(pow(snack[i].area, ) - pow(snack[i].x, ));
le[L].down = snack[i].y - sqrt(pow(snack[i].area, ) - pow(snack[i].x, ));
le[L++].i = i;
}
if (snack[i].x + snack[i].area >){
ri[R].up = snack[i].y + sqrt(pow(snack[i].area, ) - pow(( - snack[i].x), ));
ri[R].down = snack[i].y - sqrt(pow(snack[i].area, ) - pow(( - snack[i].x), ));
ri[R++].i = i;
}
} //-------------通过圆心判断各区域相交情况-------------------
for (i = ; i < n; i++)
for (j = i + ; j <= n; j++){
if (snack[i].area + snack[j].area>sqrt(pow((snack[i].x - snack[j].x), ) + pow((snack[i].y - snack[j].y), )))
map[i][j] = map[j][i] = ;
} //-----------并查集处理------------------
for (i = ; i <= n; i++)
for (j = i + ; j <= n + ; j++){
if (map[i][j]){
int x = getf(i), y = getf(j);
if (x != y)
father[y] = x;
}
} if (getf(n + ) == getf())
cout << "Bill will be bitten." << endl; else{
double Lflag = -, Rflag = -, Lup = , Ldown = , Rup = , Rdown = ; //-----------找到最高可用左边界------------------------
for (i = ; i < L; i++){
if (getf(le[i].i) == getf() && le[i].down < Lup)
Lup = le[i].down;
if (getf(le[i].i) == getf(n + ) && le[i].up >Ldown)
Ldown = le[i].up;
} if (check(le, , L) && Lup == )
Lflag = ; for (i = ; i < L; i++){
if (le[i].up <= Lup&&le[i].up >= Ldown&&check(le, le[i].up, L) && Lflag < le[i].up)
Lflag = le[i].up;
if (le[i].down <= Lup&&le[i].down >= Ldown&&check(le, le[i].down, L) && Lflag < le[i].down)
Lflag = le[i].down;
}
//---------------------------------------------------------------------- //--------------------------===右边界处理-----------------------
for (i = ; i < R; i++){
if (getf(ri[i].i) == getf() && ri[i].down < Rup)
Rup = ri[i].down;
if (getf(ri[i].i) == getf(n + ) && ri[i].up >Rdown)
Rdown = ri[i].up;
}
if (check(ri, , R) && Rup == )
Rflag = ;
for (i = ; i < R; i++){
if (ri[i].up <= Rup&&ri[i].up >= Rdown&&check(ri, ri[i].up, R) && Rflag < ri[i].up)
Rflag = ri[i].up;
if (ri[i].down <= Rup&&ri[i].down >= Rdown&&check(ri, ri[i].down, R) && Rflag < ri[i].down)
Rflag = ri[i].down;
}
if (L == )
Lflag = ;
if (R == )
Rflag = ;
if (Rflag < || Lflag < )
cout << "Bill will be bitten." << endl;
else
printf("Bill enters at (0.00, %.2lf) and leaves at (1000.00, %.2lf).\n", Lflag, Rflag);
}
}
return ;
}
[POJ 2588]--Snakes(并查集)的更多相关文章
- poj 2524 (并查集)
http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...
- [POJ 2588] Snakes
同swustoj 8 Snakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1015 Accepted: 341 ...
- poj 1456 Supermarket - 并查集 - 贪心
题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...
- poj 2492(关系并查集) 同性恋
题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...
- poj 1182 (关系并查集) 食物链
题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...
- Poj(1182),种类并查集
题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...
- Poj(1703),种类并查集
题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 2513 欧拉回路+并查集推断是否联通+Trie树
http://poj.org/problem? id=2513 最初看到 第一感觉---map 一看250000的数据量 果断放弃 然后记得曾经看过.trie取代map.尤其当数据量特别大的时候 学 ...
随机推荐
- PhoneGap笔记-01 基本使用
1. 环境配置 1.1 常用框架 jQuery Backbone.js dojo bootstrap kendo UI Sencha jQuery Mobile PhoneJS AngularJS I ...
- 【转】context和getApplicationContext()介绍
在android中常常会遇到与context有关的内容,大多都是作为参数在传递,但是它的作用究竟是什么呢 先说它的用法,举个例子 在语句 AlertDialog.Builder builder = n ...
- 利用反馈字段给帝国cms添加留言板功能(图文教程)
帝国cms的插件中提供信息反馈字段,很多人却不会用.这里谢寒教大家如何来给自己的帝国cms网站添加留言板功能 1.找到添加地址 2.添加字段 3.你可以在字段中添加多种字段类型(有文本域,单行文本框, ...
- 25_Downloading An Image
一个App,从网上下载一张图片(给出图片地址),重新命名,然后保存到手机中,再从手机中取出显示在屏幕上. 难度不大,就是找图片很蛋疼,百度搜索出来的过一会儿会失效,Google搜索出来的有些需要FQ, ...
- perl5 第六章 模式匹配
第六章 模式匹配 by flamephoenix 一.简介二.匹配操作符三.模式中的特殊字符 1.字符+ 2.字符 []和[^] 3.字符 *和? 4.转义字符 5.匹配任意字母或数字 6 ...
- mfc删除标题和边框
//删除标题和边框WS_CAPTION和WS_BORDER风格 ModifyStyle(WS_CAPTION, 0);ModifyStyle(WS_BORDER, 0);
- 使用Win32 API创建不规则形状&带透明色的窗口
前一阵突然想起了9月份电面某公司实习时的二面题,大概就是说怎么用Win32 API实现一个透明的窗口,估计当时我的脑残答案肯定让面试官哭笑不得吧.所以本人决定好好研究下这个问题.经过一下午的摸索,基本 ...
- CSS块级元素和行内元素
根据CSS规范的规定,每一个网页元素都有一个display属性,用于确定该元素的类型,每一个元素都有默认的display属性值,比如div元素,它的默认display属性值为“block”,成为“块级 ...
- tomcat的webappclassloader中一个奇怪的异常信息
假设一个应用抛出大量的Class not found信息,一般你会怀疑包冲突.但是tomcat的webappclassloader却有这种问题: 假设一个应用公布出现故障, webappclasslo ...
- DevExpress ASP.NET 使用经验谈(6)-ASPxGridView属性设置与CRUD界面优化
上一节中,我们通过简单的配置,通过ASPxGridView控件的使用,完成了对数据库表的CRUD操作. 这样的界面展现,功能是达到了,但是操作体验上,还是有所欠缺的. 图一 默认生成的列表界面 图二 ...