poj 2540 Hotter Colder (半平面交+线性规划)

Hotter Colder
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3255 Accepted: 1304

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:正方形(0,0),(0,10),(10,10),(10,0)中每次给出一个点(x,y),colder表示比上一个给出的点远,hotter表示比上一个给出的点近,same表示相同,然后求可以满足条件的范围。

题解:半平面交+线性规划

(a-x)^2+(b-y)^2>(a'-x)^2+(b'-y)^2  (a,b)表示当前点,(a',b')表示上一个点,>表示的colder,<表示的是hotter。

然后用半平面交求解可行域即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 100
#define eps 1e-8
using namespace std;
struct vector {
	double x,y;
	vector (double X=0,double Y=0) {
		x=X,y=Y;
	}
}a[N],p[N],tmp[N],line[3];
typedef vector point;
vector operator -(vector a,vector b){
	return vector (a.x-b.x,a.y-b.y);
}
vector operator +(vector a,vector b){
	return vector (a.x+b.x,a.y+b.y);
}
vector operator *(vector a,double t){
	return vector (a.x*t,a.y*t);
}
int m;
double x,y;
char s[N],last[N];
void init()
{
	m=0;
	p[m++]=point(0,0);
	p[m++]=point(10,0);
	p[m++]=point(10,10);
	p[m++]=point(0,10);
}
int dcmp(double x){
	if (fabs(x)<eps) return 0;
	return x<0?-1:1;
}
double cross(vector a,vector b)
{
	return a.x*b.y-a.y*b.x;
}
point glt(point a,point a1,point b,point b1)
{
    vector v=a1-a; vector w=b1-b;
	vector u=a-b;
	double t=cross(w,u)/cross(v,w);
	return a+v*t;	
}
void cut(point a,point b)
{
	int cnt=0;
	memset(tmp,0,sizeof(tmp));
	for (int i=0;i<m;i++){
		double c=cross(b-a,p[i]-a);
		double d=cross(b-a,p[(i+1)%m]-a);
		if (dcmp(c)>=0) tmp[cnt++]=p[i];
		if (dcmp(c)*dcmp(d)<0)
		 tmp[cnt++]=glt(a,b,p[i],p[(i+1)%m]);
	}
	m=cnt;
	for (int i=0;i<m;i++) p[i]=tmp[i];
}
int main()
{
	freopen("a.in","r",stdin);
	//freopen("my.out","w",stdout);
	int t=1;
	a[0].x=0; a[0].y=0;
	init(); bool mark=true; double area=0;
	while (scanf("%lf%lf%s",&a[t].x,&a[t].y,s+1)!=EOF){
		if (s[1]=='S') {
			printf("0.00\n"); t++; last[1]=s[1];
			mark=false;
			continue;
		}
		//init();
		if (!mark) {
		  printf("0.00\n");
		  continue;
        }
		double nowa=(a[t].x-a[t-1].x);
		double nowb=(a[t].y-a[t-1].y);
		if (dcmp(nowa)==0&&dcmp(nowb)==0) {
			if (last[1]==s[1]) printf("%.2lf\n",area);
			else {
				printf("0.00\n");
				mark=false;
			}
			last[1]=s[1];
			continue;
		}
		if (dcmp(nowa)==0) {
			double t1=(a[t].y+a[t-1].y)/2;
			line[1].x=0; line[1].y=t1;
			line[2].x=10; line[2].y=t1;
			if (dcmp(-nowb)>0&&s[1]=='H') swap(line[1],line[2]);
			if (dcmp(-nowb)<0&&s[1]=='C') swap(line[1],line[2]);
		}
		else 
		if (dcmp(nowb)==0) {
			double t1=(a[t].x+a[t-1].x)/2;
			line[1].x=t1; line[1].y=0;
			line[2].x=t1; line[2].y=10;
			if (dcmp(-nowa)>0&&s[1]=='C') swap(line[1],line[2]);
			if (dcmp(-nowa)<0&&s[1]=='H') swap(line[1],line[2]);
		}
		else 
		{
			line[2].x=1; line[2].y=(nowa*2.0-nowa*(a[t].x+a[t-1].x)-nowb*(a[t].y+a[t-1].y))/(-2.0*nowb);
			line[1].x=0; line[1].y=(-nowa*(a[t].x+a[t-1].x)-nowb*(a[t].y+a[t-1].y))/(-2.0*nowb);
			if (dcmp(-nowb)>0&&s[1]=='H') swap(line[1],line[2]);
			if (dcmp(-nowb)<0&&s[1]=='C') swap(line[1],line[2]);
	    }
		cut(line[1],line[2]);
		area=0; p[m]=p[0];
		for (int i=1;i<m;i++)
		 area+=cross(p[i]-p[0],p[i+1]-p[0]);
		area=fabs(area/2.0);
		printf("%.2lf\n",area); 
		t++; last[1]=s[1];
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值