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

Language:
Hotter Colder
Time Limit: 1000MS Memory Limit: 65536K
Total 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 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


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

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

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define eps 1e-8
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值