现在的位置: 首页 > 综合 > 正文

HDU3867 计算几何扫描线

2017年10月18日 ⁄ 综合 ⁄ 共 3712字 ⁄ 字号 评论关闭

转载自AcCry
题意:原子弹爆炸,一些互不相交的线段,求能辐射到的线段(可以将原子弹爆炸点视为泛光源)
以辐射源为中心对周围的点按照极坐标角度进行排序,然后在极坐标上使用扫描线方法。

维护一个集合,集合内的元素是与扫描线相交的线段,排序依据是线段与扫描线的交点到辐射源的距离。该集合中的最小元素就是被照射到的线段。


有关容器(set)排序依据的说明:

在扫描线运动前后,如果有两个线段存在于容器中,这两个线段与扫描线的交点到辐射源的距离远近关系不会发生变化。若发生变化,表示扫描线运动范围内两个线段有交点,与题目提供的已知条件不符。




PS:此题各种重载 各种stl

  1. #include <cstdio>  
  2. #include <cstring>  
  3. #include <cmath>  
  4. #include <set>  
  5. #include <algorithm>  
  6. using namespace std;  
  7. #define EPS 1e-8  
  8. #define LS0(a) (a << 1)  
  9. #define LS1(a) ((a << 1) | 1)  
  10.   
  11. const int MAXN = 20010;  
  12. struct Point {   
  13.     double x, y;  
  14.     Point(double _x = 0.0, double _y = 0.0): x(_x), y(_y) {}  
  15.     Point operator + (const Point &b) const {  
  16.         return Point(x + b.x, y + b.y);  
  17.     }  
  18.     Point operator - (const Point &b) const {  
  19.         return Point(x - b.x, y - b.y);  
  20.     }  
  21.     double operator ^ (const Point &b) const {  
  22.         return x * b.y - y * b.x;  
  23.     }  
  24.     bool operator < (const Point &b) const {  //逆时针  
  25.         return x * b.y < y * b.x;  
  26.     }  
  27.     void input() {  
  28.         scanf("%lf%lf", &x, &y);  
  29.     }  
  30.     double diso() {  
  31.         return sqrt(x * x + y * y);  
  32.     }  
  33.   
  34. }cur,ps[MAXN];  
  35.   
  36. Point lnlncross_pt(Point aa, Point ad, Point ba, Point bd) { // 求直线交点  
  37.     ad = ad - aa;  
  38.     bd = bd - ba;  
  39.     double tmp = bd ^ ad;  
  40.     return Point(  
  41.         (ad.x * bd.x * (ba.y - aa.y) + aa.x * bd.x * ad.y - ba.x * ad.x * bd.y) / tmp,  
  42.         (ad.y * bd.y * (aa.x - ba.x) + ba.y * ad.y * bd.x - aa.y * bd.y * ad.x) / tmp);  
  43. }  
  44.   
  45. struct Item { // 扫描线的点类型  
  46.     Point *u, *v;  
  47.     int type; // 1: 线段起点; 0: 线段终点;  
  48.     int sgid; // 线段序号  
  49.     Item(Point *_u = NULL, Point *_v = NULL, int _ty = 0, int _id = 0)  
  50.         : u(_u), v(_v), type(_ty), sgid(_id) {}  
  51.     bool operator < (const Item &b) const {  
  52.         if(u == b.u && v == b.v)  
  53.             return false;  
  54.         Point au = lnlncross_pt(Point(0.0, 0.0), cur, *u, *v);  
  55.         Point bu = lnlncross_pt(Point(0.0, 0.0), cur, *b.u, *b.v);  
  56.         return au.diso() < bu.diso();  
  57.     }  
  58.   
  59. }item[MAXN];  
  60.   
  61. bool flag[MAXN];  
  62. set<Item> Scan;  
  63.   
  64.                       
  65. bool cmp(const Item &a, const Item &b) { //极角排序 从-PI到-PI内   
  66.         return atan2(a.u->y, a.u->x) < atan2(b.u->y, b.u->x);  
  67.   
  68. }  
  69.   
  70. void inputps(int n) {  
  71.     Point src, a, b;  
  72.     src.input();  
  73.     for(int i = 0; i < n; ++i) {  
  74.         // 读取线段并求得相对于光源的坐标  
  75.         a.input(); a = a - src;  
  76.         b.input(); b = b - src;  
  77.         // 保证线段的极角序  
  78.         if(b < a) swap(a, b);  
  79.         ps[LS0(i)] = a;  
  80.         ps[LS1(i)] = b;  
  81.         item[LS0(i)] = Item(&ps[LS0(i)], &ps[LS1(i)], 0, i);  
  82.         item[LS1(i)] = Item(&ps[LS1(i)], &ps[LS0(i)], 1, i);  
  83.     }  
  84.     sort(item, item + 2 * n, cmp);  
  85. }  
  86.   
  87.   
  88.   
  89. bool sgcross_with_ax(Item &a) {   //与射线相交判断   good 以前不知道的东西  
  90.     Point tmp(-1.0, 0.0);  
  91.     return (*a.u ^ *a.v) * (*a.u ^ tmp) > 0.0  
  92.         && (*a.u ^ tmp) * (tmp ^ *a.v) > 0.0;  
  93. }  
  94.   
  95.   
  96.   
  97. int main() {  
  98.     int n;  
  99.     while(scanf("%d", &n) != EOF) {  
  100.         inputps(n);  
  101.         memset(flag,0,sizeof(flag));  
  102.         // 初始化极角扫描器  初始射线向量为(-1.0,0)  
  103.         Scan.clear();  
  104.         for(int i = 0; i < 2 * n; ++i) {  
  105.                 cur = *item[i].u;  
  106.             if(item[i].type == 1 && sgcross_with_ax(item[i]))  
  107.                 Scan.insert(item[i]);  
  108.         }  
  109.         // 极角扫描  
  110.         for(int i = 0; i < 2 * n; ++i) {  
  111.                 cur = *item[i].u;  
  112.             if(item[i].type == 1)  
  113.                 Scan.insert(item[i]);  
  114.             else  
  115.                 Scan.erase(Item(item[i].v, item[i].u, 1, item[i].sgid));  
  116.             if(!Scan.empty())  
  117.                 flag[Scan.begin()->sgid] = true;  
  118.         }  
  119.         int ans = 0;  
  120.         for(int i = 0; i < n; ++i)  
  121.             if(flag[i])ans ++;  
  122.         printf("%d\n", ans);  
  123.     }  
  124.     return 0;  
  125.   
  126. }  

抱歉!评论已关闭.