POJ2540/ZOJ1886 Hotter Colder

题意:在(0,0)到(10,10)的范围内,藏匿着某个点,一个人从起点出发,给出走到的一个点,离藏匿点近了就是“Hotter”,远了就是“Colder”,不变就是“Same” 

对于走的每个点,输出可能藏匿的面积大小。


解题思想:由于考虑的是离点远近的情况,那么关键就是两点中垂线的确定,注意在确定中垂线时的方向问题,考虑下特殊情况,比如same时的输出

半平面交模板参考kuangbin大神 http://www.cnblogs.com/kuangbin/p/3266097.html

解题思想参考了部分该博客代码 http://blog.csdn.net/mowayao/article/details/38711623

锻炼自己对O(nlogn)半平面交模板的使用

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include<string>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x)
{
    if(fabs(x) < eps) return 0;
    if(x < 0) return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x; y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x, y - b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    Point operator +(const Point &b)const
    {
        return Point(x + b.x, y + b.y);
    }

};
struct Line
{
    Point s,e;
    double k;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s; e = _e;
        k = atan2(e.y - s.y,e.x - s.x);
    }
    Point operator &(const Line &b)const
    {
        Point res = s;
        double t = ((s - b.s)^(b.s - b.e))/((s - e)^(b.s - b.e));
        res.x += (e.x - s.x)*t;
        res.y += (e.y - s.y)*t;
        return res;
    }
};
//半平面交,直线的左边代表有效区域
bool HPIcmp(Line a,Line b)
{
    if(fabs(a.k - b.k) > eps)return a.k < b.k;
    return ((a.s - b.s)^(b.e - b.s)) < 0;
}
Line Q[1010];
void HPI(Line line[], int n, Point res[], int &resn)
{
    int tot = n;
    sort(line,line+n,HPIcmp);
    tot = 1;
    for(int i = 1;i < n;i++)
        if(fabs(line[i].k - line[i-1].k) > eps)
            line[tot++] = line[i];
    int head = 0, tail = 1;
    Q[0] = line[0];
    Q[1] = line[1];
    resn = 0;
    for(int i = 2; i < tot; i++)
    {
        if(fabs((Q[tail].e-Q[tail].s)^(Q[tail-1].e-Q[tail-1].s)) < eps || fabs((Q[head].e-Q[head].s)^(Q[head+1].e-Q[head+1].s)) < eps)
            return;
        while(head < tail && (((Q[tail]&Q[tail-1]) - line[i].s)^(line[i].e-line[i].s)) > eps)
            tail--;
        while(head < tail && (((Q[head]&Q[head+1]) - line[i].s)^(line[i].e-line[i].s)) > eps)
            head++;
        Q[++tail] = line[i];
    }
    while(head < tail && (((Q[tail]&Q[tail-1]) - Q[head].s)^(Q[head].e-Q[head].s)) > eps)
        tail--;
    while(head < tail && (((Q[head]&Q[head-1]) - Q[tail].s)^(Q[tail].e-Q[tail].e)) > eps)
        head++;
    if(tail <= head + 1)return;
    for(int i = head; i < tail; i++)
        res[resn++] = Q[i]&Q[i+1];
    if(head < tail - 1)
        res[resn++] = Q[head]&Q[tail];
}
Point p[1010];
Line line[1010];
//*两点间距离
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}

Line getMidLine(Point a,Point b) {
    Point mid = (a + b);
    mid.x/=2.0;
    mid.y/=2.0;
    Point tp = b-a;
    return Line(mid, mid+Point(-tp.y, tp.x));
}
double xmult(Point p0,Point p1,Point p2){/*p0p1 X p0p2*/
    return (p1-p0)^(p2-p0);
}

int main(){

    double x,y;
    string st;
    int n=4;
    line[0]=Line( Point(0, 0),Point(10, 0));
    line[1]=Line( Point(10,0),Point(10, 10));
    line[2]=Line( Point(10,10),Point(0, 10));
    line[3]=Line( Point(0 ,10),Point(0, 0));
    Point pre = Point(0,0),cur;
    bool flag = true;
    while(cin >> x >> y >> st){
        if(!flag){
            printf("0.00\n");
            continue;
        }
        cur = Point(x,y);
        if(st[0]=='S'){
            printf("0.00\n");
            flag = false;
            continue;
        }else if(st[0]=='H'){
            line[n++] = getMidLine(cur,pre);
        }else{
            line[n++] = getMidLine(pre,cur);
        }
        pre = Point(x,y);
        int resn=0;
        double area=0;
        HPI(line,n,p,resn);
        if(resn==0){
            printf("0.00\n");
            flag = false;
        }else{
            for(int i=1;i<resn;i++)
              area+=xmult(p[0],p[i],p[(i+1)%resn]);
          if(resn<3) area=0;
          if(area<0) area=-area;
          printf("%.2f\n",area/2.0);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值