题目描述
Alice is a beauty in a robot society. So many robots want to marry her. Alice determines to marry a robot who can solve the following puzzle:
Firstly, the shape of Alice’s right palm is as follow:
And the shape of Alice’s left palm is symmetrical to her right palm.
In this puzzle, Alice will give the challenger many handprints of her palm. The challenger must correctly tell Alice each handprint is her left palm or right palm. Notice that the handprint of Alice’s palm is given by its 2D plane coordinates in clockwise or counterclockwise order. And The shape may be rotated and translated. But the shape won’t be zoomed in nor be zoomed out.
Although you are not a robot, you are interested in solving puzzles. Please try to solve this puzzle.
输入输出
看到这样的一道题,首先想到的是计算几何。于是模板先写好
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const double eps = 0.1;
const double PI = acos(-1.0);
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct point
{
double x,y;
point() {}
point(double _x,double _y)
{
x = _x;
y = _y;
}
point operator -(const point &b) const
{
return point(x - b.x,y - b.y);
}
//叉积
double operator ^(const point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const point &b)const
{
return x*b.x + y*b.y;
}
//绕原点旋转角度B(弧度值),后x,y的变化
void transXY(double B)
{
double tx = x,ty = y;
x = tx*cos(B) - ty*sin(B);
y = tx*sin(B) + ty*cos(B);
}
void input()
{
scanf("%lf%lf",&x,&y);
}
};
double dist(point a,point b)
{
return sqrt((a-b)*(a-b));
}
然后开始想,怎么区分左手和右手
然后很容易发现手掌的最下端的线段唯一为9
接着可以左右判断哪根是大拇指
于是就要用到向量中的叉积
然后还要处理顺时针和逆时针的问题
于是就想到小拇指
判一下小拇指就好了
if (fabs(dist(a[i],a[(i+1)%20])-9)<=eps)
{
point x=a[i],y=a[(i+1)%20],z=a[(i+2)%20];
if ((fabs(dist(y,z)-6)<=eps && ((y-x)^(z-y))<0)||//逆时针
(fabs(dist(y,z)-8)<=eps && ((y-x)^(z-y))>0))//顺时针
{
l=2;//右手
}
else
if ((fabs(dist(y,z)-6)<=eps && ((y-x)^(z-y))>0)||// 顺时针
(fabs(dist(y,z)-8)<=eps && ((y-x)^(z-y))<0))// 逆时针
{
l=1;//左手
}
break;
}
最后还要注意double型的精度问题,用eps控制一下,就能过了
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const double eps = 0.1;
const double PI = acos(-1.0);
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct point
{
double x,y;
point() {}
point(double _x,double _y)
{
x = _x;
y = _y;
}
point operator -(const point &b) const
{
return point(x - b.x,y - b.y);
}
//叉积
double operator ^(const point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const point &b)const
{
return x*b.x + y*b.y;
}
//绕原点旋转角度B(弧度值),后x,y的变化
void transXY(double B)
{
double tx = x,ty = y;
x = tx*cos(B) - ty*sin(B);
y = tx*sin(B) + ty*cos(B);
}
void input()
{
scanf("%lf%lf",&x,&y);
}
};
double dist(point a,point b)
{
return sqrt((a-b)*(a-b));
}
point a[25];
int n,m,i,j,k,l,o,p,T;
int main()
{
scanf("%d",&T);
while(T--)
{
for(i=0;i<20;i++) a[i].input();
l=0;
for (i=0;i<20;i++)
if (fabs(dist(a[i],a[(i+1)%20])-9)<=eps)
{
point x=a[i],y=a[(i+1)%20],z=a[(i+2)%20];
if ((fabs(dist(y,z)-6)<=eps && ((y-x)^(z-y))<0)||
(fabs(dist(y,z)-8)<=eps && ((y-x)^(z-y))>0))
{
l=2;
}
else
if ((fabs(dist(y,z)-6)<=eps && ((y-x)^(z-y))>0)||
(fabs(dist(y,z)-8)<=eps && ((y-x)^(z-y))<0))
{
l=1;
}
break;
}
if(l==1) printf("left\n");
else printf("right\n");
}
}