题目链接:http://poj.org/problem?id=2588

Snakes

Time Limit: 1000MS   Memory Limit: 65536K
 

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

  1. 3
  2. 500 500 499
  3. 0 0 999
  4. 1000 1000 200

Sample Output

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

Source

 
 
题目大意:有一个1000*1000的地图,地图上有一些蛇现在给出蛇的坐标和攻击半径(攻击范围是一个圆),Bill不能从蛇的攻击范围穿过,Bill 必须从西边进入荒漠,
     而从东边离开,可以朝任意方向四个方向走,如果能够完成,按样例格式输出 Bill 进入和离开荒漠的位置,如果有多个位置,输出最北边的位置,
     如果不能完成,输出"Bill will be bitten."
 
解题思路:这道题吧n条蛇的攻击范围,当成并查集的对象来进行,特别的吧图的最高点(n+1),最低点(0)也加入并查集,如果f[n+1]==f[0],这肯定不能到达,
     否则的话,通过判断左右边界与蛇攻击区域的相交关系,判断边界是否可行,可行的话,在此过程中找出最北方的点,具体见代码注释分析~~~
 
代码如下:
  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<cstdio>
  5. using namespace std;
  6. #define maxn 1005
  7.  
  8. /*-------------上下左右边界判断-------------*/
  9. struct UpDown{
  10. double x, y, area;
  11. }snack[maxn];
  12. struct LeftRight{
  13. double up, down;
  14. int i;
  15. }le[maxn], ri[maxn];
  16.  
  17. int father[maxn], map[maxn][maxn];
  18.  
  19. //----------检查当前出入最高点是否可用--------------------
  20. bool check(LeftRight *p, double x, int n){
  21. for (int i = ; i < n; i++){
  22. if (p[i].up>x&&p[i].down < x)
  23. return false;
  24. }
  25. return true;
  26. }
  27.  
  28. //-------------并查集判断构造的点(包括上下边界)是否连通-------------------
  29. int getf(int x){
  30. return father[x] != x ? father[x] = getf(father[x]) : x;
  31. }
  32.  
  33. int main()
  34. {
  35. int n, i, j;
  36. while (cin >> n){
  37. for (i = ; i <= n + ; i++)
  38. father[i] = i;
  39. int L = , R = ;
  40. memset(map, , sizeof(map));
  41. for (i = ; i <= n; i++){
  42. cin >> snack[i].x >> snack[i].y >> snack[i].area;
  43.  
  44. //-----------建立上下关系---------------
  45. if (snack[i].y + snack[i].area > )
  46. map[][i] = map[i][] = ;
  47. if (snack[i].y - snack[i].area < )
  48. map[n + ][i] = map[i][n + ] = ;
  49.  
  50. //---------建立左右关系------------------
  51. if (snack[i].x - snack[i].area < ){
  52. le[L].up = snack[i].y + sqrt(pow(snack[i].area, ) - pow(snack[i].x, ));
  53. le[L].down = snack[i].y - sqrt(pow(snack[i].area, ) - pow(snack[i].x, ));
  54. le[L++].i = i;
  55. }
  56. if (snack[i].x + snack[i].area >){
  57. ri[R].up = snack[i].y + sqrt(pow(snack[i].area, ) - pow(( - snack[i].x), ));
  58. ri[R].down = snack[i].y - sqrt(pow(snack[i].area, ) - pow(( - snack[i].x), ));
  59. ri[R++].i = i;
  60. }
  61. }
  62.  
  63. //-------------通过圆心判断各区域相交情况-------------------
  64. for (i = ; i < n; i++)
  65. for (j = i + ; j <= n; j++){
  66. if (snack[i].area + snack[j].area>sqrt(pow((snack[i].x - snack[j].x), ) + pow((snack[i].y - snack[j].y), )))
  67. map[i][j] = map[j][i] = ;
  68. }
  69.  
  70. //-----------并查集处理------------------
  71. for (i = ; i <= n; i++)
  72. for (j = i + ; j <= n + ; j++){
  73. if (map[i][j]){
  74. int x = getf(i), y = getf(j);
  75. if (x != y)
  76. father[y] = x;
  77. }
  78. }
  79.  
  80. if (getf(n + ) == getf())
  81. cout << "Bill will be bitten." << endl;
  82.  
  83. else{
  84. double Lflag = -, Rflag = -, Lup = , Ldown = , Rup = , Rdown = ;
  85.  
  86. //-----------找到最高可用左边界------------------------
  87. for (i = ; i < L; i++){
  88. if (getf(le[i].i) == getf() && le[i].down < Lup)
  89. Lup = le[i].down;
  90. if (getf(le[i].i) == getf(n + ) && le[i].up >Ldown)
  91. Ldown = le[i].up;
  92. }
  93.  
  94. if (check(le, , L) && Lup == )
  95. Lflag = ;
  96.  
  97. for (i = ; i < L; i++){
  98. if (le[i].up <= Lup&&le[i].up >= Ldown&&check(le, le[i].up, L) && Lflag < le[i].up)
  99. Lflag = le[i].up;
  100. if (le[i].down <= Lup&&le[i].down >= Ldown&&check(le, le[i].down, L) && Lflag < le[i].down)
  101. Lflag = le[i].down;
  102. }
  103. //----------------------------------------------------------------------
  104.  
  105. //--------------------------===右边界处理-----------------------
  106. for (i = ; i < R; i++){
  107. if (getf(ri[i].i) == getf() && ri[i].down < Rup)
  108. Rup = ri[i].down;
  109. if (getf(ri[i].i) == getf(n + ) && ri[i].up >Rdown)
  110. Rdown = ri[i].up;
  111. }
  112. if (check(ri, , R) && Rup == )
  113. Rflag = ;
  114. for (i = ; i < R; i++){
  115. if (ri[i].up <= Rup&&ri[i].up >= Rdown&&check(ri, ri[i].up, R) && Rflag < ri[i].up)
  116. Rflag = ri[i].up;
  117. if (ri[i].down <= Rup&&ri[i].down >= Rdown&&check(ri, ri[i].down, R) && Rflag < ri[i].down)
  118. Rflag = ri[i].down;
  119. }
  120. if (L == )
  121. Lflag = ;
  122. if (R == )
  123. Rflag = ;
  124. if (Rflag < || Lflag < )
  125. cout << "Bill will be bitten." << endl;
  126. else
  127. printf("Bill enters at (0.00, %.2lf) and leaves at (1000.00, %.2lf).\n", Lflag, Rflag);
  128. }
  129. }
  130. return ;
  131. }
 

[POJ 2588]--Snakes(并查集)的更多相关文章

  1. poj 2524 (并查集)

    http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...

  2. [POJ 2588] Snakes

    同swustoj 8 Snakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1015   Accepted: 341 ...

  3. poj 1456 Supermarket - 并查集 - 贪心

    题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...

  4. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  5. poj 1182 (关系并查集) 食物链

    题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...

  6. Poj(1182),种类并查集

    题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...

  7. Poj(1703),种类并查集

    题目链接:http://poj.org/problem?id=1703 已经不是第一次接触种类并查集了,直到今天才搞懂. 感谢红黑联盟,感谢杰哥!!! 每个节点只要关系确定,不管是不是同一个集合里面, ...

  8. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  9. poj 2513 欧拉回路+并查集推断是否联通+Trie树

    http://poj.org/problem? id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得曾经看过.trie取代map.尤其当数据量特别大的时候 学 ...

随机推荐

  1. JS字符串方法总结整理

    //javascript字符串方法总结   1.String.charAt(n)      //取得字符串中的第n个字符   2.String.charCodeAt(n)  //取得字符串中第n个字符 ...

  2. CSS Select 标签取选中文本值

    $("#userDep").find("option:selected").text()

  3. Latex调整行距

    修改行间距的方法: \usepackage{setspace}%使用间距宏包 \begin{document} \begin{spacing}{2.0}%%行间距变为double-space 双倍行距 ...

  4. 一个Sqrt函数引发的血案(转)

    作者: 码农1946  来源: 博客园  发布时间: 2013-10-09 11:37  阅读: 4556 次  推荐: 41   原文链接   [收藏]   好吧,我承认我标题党了,不过既然你来了, ...

  5. 读书笔记:js设计模式

    面向过程编程,面向对象编程和函数式编程> 定义一个类方法1:function Anim(){ } Anim.prototype.start = function(){ .. };Anim.pro ...

  6. android学习----overridePendingTransition

    1 Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画. 它包括两个部分:一部分是第一个activity退出时的动画:另外一部分时第二个activity进入时 ...

  7. 贫血模型or领域模型

    参考: http://lifethinker.iteye.com/blog/283668 http://www.uml.org.cn/mxdx/200907132.asp http://www.itu ...

  8. JAVA GUI学习 - JOptionPane对话框组件学习

    /** * 对话框 - 学习笔记 * @author Wfei * */ public class JoptionPaneKnow extends JFrame { public JoptionPan ...

  9. STL algorithm算法mismatch(37)

    mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...

  10. Python lambda和reduce函数

    看到一篇博文写lambda和reduce函数.笔者小痒了一下,用Python实现一下: #! /usr/bin/env python # -*-coding:utf-8-*- import time ...