POJ 2540 Hotter Colder(半平面交)

Hotter Colder
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2550 Accepted: 1058

Description

The children's game Hotter Colder is played as follows. Player A leaves the room while player B hides an object somewhere in the room. Player A re-enters at position (0,0) and then visits various other positions about the room. When player A visits a new position, player B announces "Hotter" if this position is closer to the object than the previous position; player B announces "Colder" if it is farther and "Same" if it is the same distance.

Input

Input consists of up to 50 lines, each containing an x,y coordinate pair followed by "Hotter", "Colder", or "Same". Each pair represents a position within the room, which may be assumed to be a square with opposite corners at (0,0) and (10,10).

Output

For each line of input print a line giving the total area of the region in which the object may have been placed, to 2 decimal places. If there is no such region, output 0.00.

Sample Input

10.0 10.0 Colder
10.0 0.0 Hotter
0.0 0.0 Colder
10.0 10.0 Hotter

Sample Output

50.00
37.50
12.50
0.00

Source


题意:每次小孩可以到一个新的点,然后另一个人会告诉他他离目标点的距离离上一点是变大了还是变小了。问每次可以确定目标点放的位置面积

分析:每进行一次移动可以确定在两个点的中垂线的哪侧或者就在中垂线上。每次都求一次半平面交即可。半平面交用的是白书上O(nlogn)的算法,还是不错的。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1.0);
const double MAXN = 1000000.0;
struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vec;
//向量+向量 = 向量,点+向量 = 点
Vec operator +(Vec A,Vec B){return Vec(A.x+B.x,A.y+B.y);}
//点-点 = 向量
Vec operator -(Point A,Point B){return Vec(A.x-B.x,A.y-B.y);}
//向量*数 = 向量
Vec operator *(Vec A,double p){return Vec(A.x*p,A.y*p);}
//向量/数 = 向量
Vec operator /(Vec A,double p){return Vec(A.x/p,A.y/p);}
bool operator <(const Point& a,const Point& b){
    return a.x<b.x || (a.x == b.x && a.y<b.y);
}
const double EPS = 1e-10;
int dcmp(double x){
    if(fabs(x)<EPS) return 0;else return x<0? -1: 1;
}
bool operator == (const Point& a,const Point &b){
    return dcmp(a.x-b.x)==0 &&dcmp(a.y-b.y) == 0;
}
/*==========以上为基本定义============*/
double Dot(Vec A,Vec B){return A.x*B.x+A.y*B.y;}
double Length(Vec A){ return sqrt(Dot(A,A));}
double Angle(Vec A,Vec B){return acos(Dot(A,B)/Length(A)/Length(B));}
/*==========用点积算向量长度和两个向量夹角============*/
double Cross(Vec A,Vec B){return A.x*B.y - A.y*B.x;}
//ABC的三角形有向面积的两倍
double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}
//rad是弧度 逆时针旋转
Vec Rotate(Vec A,double rad){
    return Vec(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
//逆时针旋转90°的单位法向量
Vec Normal(Vec A){
    double L = Length(A);
    return Vec(-A.y/L,A.x/L);
}
/*=====以上为叉积的基本运算=====*/
//P+tv和Q+tw两条直线的交点,确保有唯一交点
Point GetlineIntersection(Point P,Vec v,Point Q,Vec w){
    Vec u = P-Q;
    double t = Cross(w,u)/Cross(v,w);
    return P+v*t;
}
//点到直线的距离
double DistanceToLine(Point P,Point A,Point B){
    Vec v1 = B-A,v2 = P-A;
    return fabs(Cross(v1,v2)/Length(v1));//不取绝对值那么得到的是有向距离
}
//点到线段的距离
double DistanceToSegment(Point P,Point A,Point B){
    if(A == B)return Length(P-A);
    Vec v1 = B - A, v2 = P - A, v3 = P - B;
    if(dcmp(Dot(v1,v2))<0)return Length(v2);
    else if(dcmp(Dot(v1,v3)>0)) return Length(v3);
    else return fabs(Cross(v1,v2))/Length(v1);
}
//点在直线上的投影
Point GetLineProjection(Point P,Point A,Point B){
    Vec v = B - A;
    return A + v*(Dot(v,P-A) / Dot(v,v));
}
//判断两条线段是否相交 此处必须为规范相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){
    double c1 = Cross(a2-a1,b1-a1),c2 = Cross(a2-a1,b2-a1);
    double c3 = Cross(b2-b1,a1-b1),c4 = Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
//如果允许端点相交,则用以下代码,判断一个点是否在一条线段上
bool OnSegment(Point p,Point a1,Point a2){
    return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;
}
/*=========以上为点和直线,直线和直线关系的内容========*/
//多边形有向面积
double PolygonArea(Point* p,int n){
    double area = 0;
    for(int i=1;i<n-1;i++)
        area += Cross(p[i] - p[0],p[i+1]-p[0]);
    return area/2;
}
double isint(double x){
    return fabs(x - (int)(x+0.5))<EPS;
}
/*========多边形面积等内容=========*/
struct Line{
    Point P;
    Vec v;
    double ang;
    Line(){}
    Line(Point P,Vec v):P(P),v(v){ang  = atan2(v.y,v.x);}
    bool operator < (const Line& L) const {
        return ang < L.ang;
    }
};
bool OnLeft(Line L,Point P){
    return Cross(L.v,P - L.P)>=0;          //如果线上的点不算就改成>
}
Point GetIntersection(Line a, Line b){
    Vec u = a.P - b.P;
    double t = Cross(b.v,u)/Cross(a.v,b.v);
    return a.P+a.v*t;
}
int HalfplaneIntersection(Line* L,int n,Point* poly){
    sort(L,L+n);
    int first,last;
    Point *p = new Point[n];
    Line *q = new Line[n];
    q[first = last = 0] = L[0];
    for(int i=1;i<n;i++){
        while(first<last && !OnLeft(L[i],p[last-1]))last--;
        while(first<last && !OnLeft(L[i],p[first]))first++;
        q[++last] = L[i];
        if(fabs(Cross(q[last].v,q[last-1].v))<EPS){
            last--;
            if(OnLeft(q[last],L[i].P))q[last] = L[i];
        }
        if(first<last) p[last-1] = GetIntersection(q[last-1],q[last]);
    }
    while(first<last && !OnLeft(q[first],p[last-1]))last -- ;
    if(last-first<=1)return 0;
    p[last] = GetIntersection(q[last],q[first]);
    int m = 0;
    for(int i=first;i<=last;i++)poly[m++] = p[i];
    return m;
}
/*=========半平面交所需函数及主过程=========*/
Line L[105];
Point Poly[105];
int main()
{
    Point a,b;
    double x,y;
    int n = 0;
    string pd;
    a = Point(0,0);
    Point k1 = Point(0,0),k2 = Point(0,10),k3 = Point(10,10),k4 = Point(10,0);
    L[n++] = Line(k1,k4-k1),L[n++] = Line(k4,k3-k4);
    L[n++] = Line(k3,k2-k3),L[n++] = Line(k2,k1-k2);
    while(scanf("%lf%lf",&x,&y)==2){
        b = Point(x,y);
        Point temp = (a+b)/2;
        cin>>pd;
        if(pd == "Colder")L[n++] = Line(temp,Normal(b-a));
        else if(pd == "Hotter")L[n++] = Line(temp,Normal(a-b));
        else L[n++] = Line(temp,Normal(b-a)),L[n++] = Line(temp,Normal(a-b));
        int ok = HalfplaneIntersection(L,n,Poly);
        if(ok<=2)printf("0.00\n");
        else printf("%.2lf\n",fabs(PolygonArea(Poly,ok)));
        a = b;
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值