poj2540Hotter Colder【半平面交求线性规划面积】

来源:互联网 发布:淘宝图片空间全部删除 编辑:程序博客网 时间:2024/05/16 18:38

Language:
Hotter Colder
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2966 Accepted: 1195

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 Colder10.0 0.0 Hotter0.0 0.0 Colder10.0 10.0 Hotter

Sample Output

50.0037.5012.500.00


题意:起点(0,0)给出下一步一步要走的点并给出该点相对上一个点离目标是更近了还是更远了求出目标物所在区域的可能面积

解题思路:作图即可看出所在区域的面积与所在位置的关系线性规划用半平面交求面积

#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#define eps 1e-8using namespace std;struct point{double x,y;char str[10];}A[55],p[55],q[55];int n=1,endcnt,tempcnt;int seg(double k){if(fabs(k)<eps)return 0;return k>0?1:-1;}point getinter(point p1,point p2,double a,double b,double c){double u=fabs(a*p1.x+b*p1.y+c);double v=fabs(a*p2.x+b*p2.y+c);point temp;temp.x=(p1.x*v+p2.x*u)/(u+v);temp.y=(p1.y*v+p2.y*u)/(u+v);return temp;}void getline(double &a,double &b,double &c){double xx=(A[n].y-A[n-1].y);double yy=(A[n-1].x-A[n].x);b=-xx;a=yy;double x1=(A[n].x+A[n-1].x)/2.0;double y1=(A[n].y+A[n-1].y)/2.0;c=-(a*x1+b*y1);double judge=A[n-1].x*a+A[n-1].y*b+c;if(A[n].str[0]=='C'){if(seg(judge)<0){a=-a;b=-b;c=-c;}}else if(A[n].str[0]=='H'){if(seg(judge)>0){a=-a;b=-b;c=-c;}}}void slove(){double a,b,c;int tempcnt=0;getline(a,b,c);for(int i=1;i<=endcnt;++i){if(a*p[i].x+b*p[i].y+c>=0)q[++tempcnt]=p[i];else {if(a*p[i-1].x+b*p[i-1].y+c>0){q[++tempcnt]=getinter(p[i],p[i-1],a,b,c);}if(a*p[i+1].x+b*p[i+1].y+c>0){q[++tempcnt]=getinter(p[i],p[i+1],a,b,c);}}}for(int i=1;i<=tempcnt;++i){p[i]=q[i];}p[tempcnt+1]=p[1];p[0]=p[tempcnt];endcnt=tempcnt;double area=0;for(int i=1;i<=endcnt;++i){area+=p[i].x*p[i+1].y-p[i].y*p[i+1].x;}printf("%.2lf\n",fabs(area)/2.0);}int main(){A[0].x=A[0].y=0;p[1].x=0;p[1].y=0;p[2].x=0;p[2].y=10;p[3].x=10;p[3].y=10;p[4].x=10;p[4].y=0;p[5]=p[1];p[0]=p[4];endcnt=4;int flag=0;while(scanf("%lf %lf %s",&A[n].x,&A[n].y,A[n].str)!=EOF){if(A[n].str[0]=='S'||flag){printf("%.2lf\n",0);flag=1;continue;}slove();n++;}return 0;}


0 0
原创粉丝点击