Hotter Colder
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];
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/32426.html