poj2540Hotter Colder(半平面交)

链接

根据距离可以列得直线方程,附上初始矩形的四个顶点,依次用直线切割。

  1 #include<iostream>
  2 #include <stdio.h>
  3 #include <math.h>
  4 #include<cstring>
  5 #include<algorithm>
  6 #define eps 1e-8
  7 using namespace std;
  8 const int MAXN=1550;
  9 int m;
 10 double r,tx,ty,ans;
 11 int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数
 12 struct point
 13 {
 14     double x,y;
 15     point(double x=0,double y=0):x(x),y(y) {}
 16 };
 17 point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点
 18 void getline(point x,point y,double &a,double &b,double   &c) //两点x、y确定一条直线a、b、c为其系数
 19 {
 20     a = y.y - x.y;
 21     b = x.x - y.x;
 22     c = y.x * x.y - x.x * y.y;
 23 }
 24 void initial()
 25 {
 26     for(int i = 1; i <= m; ++i)p[i] = points[i];
 27     p[m+1] = p[1];
 28     p[0] = p[m];
 29     cCnt = m;//cCnt为最终切割得到的多边形的顶点数,将其初始化为多边形的顶点的个数
 30 }
 31 point intersect(point x,point y,double a,double b,double c) //求x、y形成的直线与已知直线a、b、c、的交点
 32 {
 33     //cout<<a<<" "<<b<<" "<<c<<endl;
 34     double u = fabs(a * x.x + b * x.y + c);
 35     double v = fabs(a * y.x + b * y.y + c);
 36     // cout<<u<<" "<<v<<endl;
 37     point pt;
 38     pt.x=(x.x * v + y.x * u) / (u + v);
 39     pt.y=(x.y * v + y.y * u) / (u + v);//cout<<pt.x<<" -"<<pt.y<<" "<<x.x<<" "<<y.x<<endl;
 40     return  pt;
 41 }
 42 void cut(double a,double b ,double c)
 43 {
 44     curCnt = 0;
 45     for(int i = 1; i <= cCnt; ++i)
 46     {
 47         if(a*p[i].x + b*p[i].y + c > -eps)q[++curCnt] = p[i];
 48         else
 49         {
 50             if(a*p[i-1].x + b*p[i-1].y + c > eps)
 51             {
 52                 q[++curCnt] = intersect(p[i-1],p[i],a,b,c);
 53             }
 54             if(a*p[i+1].x + b*p[i+1].y + c > eps) //原理同上
 55             {
 56                 q[++curCnt] = intersect(p[i],p[i+1],a,b,c);
 57             }
 58         }
 59         //cout<<q[curCnt].x<<" --"<<q[curCnt].y<<" "<<i<<" "<<p[i].x<<" "<<p[i].y<<endl;
 60     }
 61     for(int i = 1; i <= curCnt; ++i)p[i] = q[i];
 62     p[curCnt+1] = q[1];
 63     p[0] = p[curCnt];
 64     cCnt = curCnt;
 65 }
 66 int dcmp(double x)
 67 {
 68     if(fabs(x)<eps) return 0;
 69     return x<0?-1:1;
 70 }
 71 void solve(double x,double y,int flag)
 72 {
 73     double a,b,c;
 74     if(flag == 1)
 75     {
 76         a = -2*x+2*tx,b = -2*y+2*ty,c = x*x+y*y-tx*tx-ty*ty;
 77     }
 78     else if(flag==2)
 79     {
 80         a = 2*x-2*tx,b = 2*y-2*ty,c = tx*tx+ty*ty-x*x-y*y;
 81     }
 82 //    if(dcmp(a)==0&&dcmp(b)==0&&dcmp(c)<=0)
 83 //    {
 84 //        ans = 0;
 85 //        return ;
 86 //    }
 87     cut(a,b,c);
 88 }
 89 int main()
 90 {
 91     points[1] = point(0,0);
 92     points[2] = point(0,10);
 93     points[3] = point(10,10);
 94     points[4] = point(10,0);
 95     p[5] = p[1];
 96     m = 4;
 97     tx = 0,ty=0;
 98     char s[10];
 99     double x,y;
100     initial();
101     while(scanf("%lf%lf%s",&x,&y,s)!=EOF)
102     {
103         int flag;
104         if(strcmp(s,"Colder")==0||strcmp(s,"Same")==0)solve(x,y,1);
105         if(strcmp(s,"Hotter")==0||strcmp(s,"Same")==0)solve(x,y,2);
106         tx = x,ty=y;
107         double area = 0;
108         for(int i = 1; i <= cCnt; ++i)
109         {
110             //cout<<p[i].x<<" "<<p[i].y<<endl;
111             area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y;
112         }
113         area = fabs(area / 2.0);
114         ans = area;
115         printf("%.2f\n",ans);
116     }
117 }

poj2540Hotter Colder(半平面交),布布扣,bubuko.com

时间: 2024-08-24 14:05:57

poj2540Hotter Colder(半平面交)的相关文章

POJ2540-Hotter Colder(半平面交)

Hotter Colder Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2523   Accepted: 1045 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

POJ 2540 半平面交求可行区域面积

Hotter Colder Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2343   Accepted: 981 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

LA 2218 (半平面交) Triathlon

题意: 有n个选手,铁人三项有连续的三段,对于每段场地选手i分别以vi, ui 和 wi匀速通过. 对于每个选手,问能否通过调整每种赛道的长度使得他成为冠军(不能并列). 分析: 粗一看,这不像一道计算几何的题目. 假设赛道总长度是1,第一段长x,第二段长y,第三段则是1-x-y 那么可以计算出每个选手完成比赛的时间Ti 对于选手i,若要成为冠军则有Ti < Tj (i ≠ j) 于是就有n-1个不等式,每个不等式都代表一个半平面. 在加上x>0, y>0, 1-x-y>0 这三个

POJ3525-Most Distant Point from the Sea(二分+半平面交)

Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3955   Accepted: 1847   Special Judge Description The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural t

bzoj 1007 水平可见直线 半平面交稀里糊涂的过了...

题意:按y=Ax+B的形式给出n(<=50000)条直线,求从y值为无穷大的地方向下看能看到的直线编号 一看到题目就想到半平面交,以每条直线的上方为一个半平面,求半平面的交,交集中存在的直线就是能看到的直线 但是写出来之后发现样例都过不了... 对于样例,如果允许半平面在边界处重叠那么答案是1,2,3,如果不允许只有1.. 然后抱着试一试的心理交上去了,结果竟然直接AC了.. 后来看题解只需要考虑交点x坐标.. bzoj 1007 水平可见直线 半平面交稀里糊涂的过了...,布布扣,bubuko

poj 3525 Most Distant Point from the Sea 半平面交 + 二分

题目来源: http://poj.org/problem?id=3525 分析: 题意:给定一个凸多边形,求多边形中距离边界最远的点到边界的距离. 思路 : 每次将凸多边形每条边往里平移d,判断是否存在核:二分d即可. 多边形边上的点(x , y)往里平移d 后的 坐标: s , e  为向量的 起点和终点, len 为起点和终点的距离, h 为平移的距离 x' = x + dx y' = y + dy dx = ( s.y - e.y ) / len * h ,( 原理 是 利用 三角形的相似

UVALive 4992 Jungle Outpost(半平面交)

题意:给你n个塔(点)形成一个顺时针的凸包,敌人可以摧毁任何塔,摧毁后剩下的塔再组成凸包 在开始的凸包内选一点为主塔,保证敌人摧毁尽量多塔时主塔都还在现在的凸包内,求出最多摧毁的塔 题解:这题关键就是选的主塔在不同的地方,敌人就会摧毁不同的塔来让你的主塔暴露 因此这样想,找出敌人摧毁不同的塔后形成的所有不同的凸包,再求出所有凸包的交就好 具体就是,首先枚举摧毁塔的个数k,再把摧毁任意k个塔所形成的所有不同的凸包求一个交,如果为空就代表了摧毁k个塔一定可以保证无论主塔在哪儿都可以暴露(关键) 而所

poj 1279 半平面交核面积

Art Gallery Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6668   Accepted: 2725 Description The art galleries of the new and very futuristic building of the Center for Balkan Cooperation have the form of polygons (not necessarily conve

poj 3335 Rotating Scoreboard(半平面交)

Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6420   Accepted: 2550 Description This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the ed