Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71859
  • 博文数量: 41
  • 博客积分: 1475
  • 博客等级: 上尉
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 22:49
文章分类
文章存档

2012年(8)

2011年(1)

2009年(32)

我的朋友
最近访客

分类: C/C++

2012-03-23 12:44:40

题目: 

思路:

注意浮点数精度以及圆的内切外切。

代码:

点击(此处)折叠或打开

  1. //
  2. // Author: xfye
  3. // Status: AC
  4. //

  5. #include <iostream>
  6. #include <cmath>
  7. #include <cstring>
  8. #include <cstdlib>

  9. using namespace std;

  10. #define EPSINON 1e-6

  11. int compareFloat(double f1, double f2)
  12. {
  13.     if (fabs(f1 - f2) < EPSINON) {
  14.         return 0; // equal
  15.     }

  16.     if (f1 - f2 < -EPSINON) {
  17.         return -1; // f1 < f2
  18.     }

  19.     return 1; // f1 > f2
  20. }


  21. int g_max = 0;

  22. struct Ring
  23. {
  24.     double x;
  25.     double y;
  26.     double r;
  27.     int visited;
  28. };

  29. //
  30. // Becarefull that all rings are of the doughnut kind (with a hole in them)
  31. //
  32. bool isTwoRingOverlapped(Ring* ring1, Ring* ring2)
  33. {
  34.     double distance = sqrt(( ring1->x - ring2->x ) * ( ring1->x - ring2->x ) + ( ring1->y - ring2->y ) * ( ring1->y - ring2->y ) );
  35.     //cout << "ring1->r: " << ring1->r << endl;
  36.     //cout << "ring2->r: " << ring2->r << endl;
  37.     //cout << "distance: " << distance << endl;

  38.     if (compareFloat(ring1->x, ring2->x) == 0 &&
  39.         compareFloat(ring1->y, ring2->y) == 0 &&
  40.         compareFloat(ring1->r, ring2->r) == 0) {
  41.         return true;
  42.     }

  43.     if (compareFloat(distance, fabs(ring1->r - ring2->r)) < 0) {
  44.         return false;
  45.     }

  46.     if (compareFloat(distance, ring1->r + ring2->r) > 0) {
  47.         return false;
  48.     }

  49.     return true;
  50. }

  51. //
  52. // Calculate the connections from rings[start] to the
  53. // rings behind rings[start].
  54. //
  55. int calcConnections(Ring* rings, int ringsNum, int start)
  56. {
  57.     int ret = 1;

  58.     rings[start].visited = 1;
  59.     for (int i = 0; i < ringsNum; i++) {
  60.         if (i == start || rings[i].visited) {
  61.             continue;
  62.         }
  63.         //cout << start << "," << i << endl;
  64.         if (isTwoRingOverlapped(&rings[start], &rings[i])) {
  65.             if (!rings[i].visited && start < i) {
  66.                 ret += calcConnections(rings, ringsNum, i);
  67.             } else {
  68.                 ret++;
  69.             }
  70.         }
  71.     }

  72.     if (g_max < ret) {
  73.         g_max = ret;
  74.     }

  75.     return ret;
  76. }

  77. int main()
  78. {
  79.     int ringsNum = 0;
  80.     Ring rings[100];


  81.     cin >> ringsNum;
  82.     while (ringsNum != -1) {
  83.         memset(rings, 0, sizeof(rings));
  84.         g_max = 0;
  85.         for (int i = 0; i < ringsNum; i++) {
  86.             cin >> rings[i].x >> rings[i].y >> rings[i].r;
  87.         }

  88.         for (int i = 0; i < ringsNum; i++) {
  89.             if (!rings[i].visited) {
  90.                 calcConnections(rings, ringsNum, i);
  91.             }
  92.         }

  93.         cout << "The largest component contains " << g_max;
  94.         if (g_max == 1) {
  95.             cout << " ring." << endl;
  96.         } else {
  97.             cout << " rings." << endl;
  98.         }
  99.         cin >> ringsNum;
  100.     }
  101. }

阅读(378) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~