2020牛客暑期多校训练营Operation Love(计算几何)

Operation Love

题目描述

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.

输入描述:

在这里插入图片描述

输出描述:

在这里插入图片描述

示例1

输入

2
1.000000 0.000000
10.000000 0.000000
10.000000 8.000000
9.000000 8.000000
9.000000 5.000000
8.000000 5.000000
8.000000 10.000000
7.000000 10.000000
7.000000 5.000000
6.000000 5.000000
6.000000 10.000000
5.000000 10.000000
5.000000 5.000000
4.000000 5.000000
4.000000 10.000000
3.000000 10.000000
3.000000 3.000000
2.000000 3.000000
2.000000 6.000000
1.000000 6.000000
-1.000123 0.000000
-10.000123 0.000000
-10.000123 8.000000
-9.000123 8.000000
-9.000123 5.000000
-8.000123 5.000000
-8.000123 10.000000
-7.000123 10.000000
-7.000123 5.000000
-6.000123 5.000000
-6.000123 10.000000
-5.000123 10.000000
-5.000123 5.000000
-4.000123 5.000000
-4.000123 10.000000
-3.000123 10.000000
-3.000123 3.000000
-2.000123 3.000000
-2.000123 6.000000
-1.000123 6.000000

输出

right
left

示例2

输入

1
19.471068 -6.709056
13.814214 -1.052201
13.107107 -1.759308
15.228427 -3.880629
14.521320 -4.587735
10.985786 -1.052201
10.278680 -1.759308
13.814214 -5.294842
13.107107 -6.001949
9.571573 -2.466415
8.864466 -3.173522
12.400000 -6.709056
11.692893 -7.416162
8.157359 -3.880629
7.450253 -4.587735
12.400000 -9.537483
11.692893 -10.244590
9.571573 -8.123269
8.864466 -8.830376
13.107107 -13.073017

输出

right

说明

在这里插入图片描述

题目大意

给定20个坐标,这些坐标可以围成一个手掌,手掌的形状和大小都不变,但是会旋转和平移,坐标以顺时针或者逆时针的顺序给出。要求你判断这是左手还是右手。

分析

由于形状大小不变,可以找左手和右手的各自的特征。如图:
在这里插入图片描述
一拍脑瓜可知,只有掌根部分的长度为9。因此可以以此为突破口。首先遍历每条边。可以很简单地找到这条边。然后考虑相邻的两条。
分类讨论:
当坐标顺时针给出的时候。
1、下一条边是6,则就如上图所示,是右手。
2、下一条边是8,则刚好与上图相反,是左手。
当坐标逆时针给出的时候。
1、下一条边是6,则是左手。
2、下一条边是8,则是右手。(自己画图)

接下来是判断是顺时针给的坐标还是逆时针给的坐标。
可以直接用叉积判断,从9这条边的向量起点到终点与下一条边的起点到终点的向量进行叉积,若是正的则是逆时针给出的。否则是顺时针。
用右手螺旋定则即可。

接下来就是代码。

代码

#include<bits/stdc++.h>
using namespace std;
const double eps=0.1;
const double PI=acos(-1.0);
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;
    }
    void input(){
        scanf("%lf%lf",&x,&y);
    }
};
double dist(point a,point b){
  return sqrt((a-b)*(a-b));
}
point a[25];
int main()
{
    int t;scanf("%d",&t);
    while(t--){
        for(int i=0;i<20;i++) a[i].input();
        int fl=0;//1 left   2 right
        for(int 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)){
                    fl=2;
                }//当顺时针且下一条为6或逆时针且下一条为8时为右手
                else if((fabs(dist(y,z)-6)<=eps&&((y-x)^(z-y))>0)||(fabs(dist(y,z)-8)<=eps&&((y-x)^(z-y))<0)){
                    fl=1;
                }//当顺时针且下一条为8或逆时针且下一条为6时为右手
                break;
            }
        }//枚举,注意eps精度问题
        if(fl==1) printf("left\n");
        else printf("right\n");
    }
}

END

猜你喜欢

转载自blog.csdn.net/zhangchizc/article/details/107443241
今日推荐