C.Operation Love - 2020牛客暑期多校训练营(第三场)- 计算几何

C.Operation Love

链接:https://ac.nowcoder.com/acm/contest/5668/C
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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:

img

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.

输入描述:

The first line contains one integer t (1 ≤≤ t ≤≤ 103103) --- the number of handprints in Alice's puzzle.

Each handprint is described by a simple polygon composed of 20 points. Each point in this polygon will be given in clockwise or counterclockwise order. Each line contains two real numbers with exactly six digits after the decimal point, representing the coordinate of a point. So a handprint is composed of 20 lines in the input.

All values of coordinate in the input is in the range [-1000.0000000, 1000.000000].

输出描述:

For each footprint of palm, print a line contains "right" or "left", indicating the footprint is the right palm or the left palm respectively.

示例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

说明

The handprint of example 2 is as follows:

img

It obviously is the right palm.

题解

截屏2020-07-18 下午11.37.52

在图中标注各段长度如图,注意到,长度为6和9的线段相邻且都是唯一的

考虑到标记ABC三点如图所示

设向量\(\vec{AC} = \vec{n}\),向量\(\vec{BC} = \vec{m}\)

根据计算几何[叉积判断向量的旋转方向]

对于右手,\(\vec{n}\)需要顺时针旋转到\(\vec{m}\)

对于左手,\(\vec{n}\)需要逆时针旋转到\(\vec{m}\)

因此,整体思路就是

  • 找到长度为6和长度为9的线段
  • 确定A、B、C
  • 求出向量\(\vec{n}\),\(\vec{m}\)
  • 叉积判断\(\vec{n}\),\(\vec{m}\)旋转关系
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>

#define MaxN 20+5
#define EPS 1e-3
// EPS不能太小,这一题数据EPS太小会WA
using namespace std;
double p[MaxN][2];   // 第i个点的坐标为 ( p[i][0],p[i][1] )

inline double get_dis(int i,int j){ // 返回第i个顶点和第j个顶点之间的距离的平方
    return (p[i][0]-p[j][0]) * (p[i][0]-p[j][0]) + (p[i][1]-p[j][1]) * (p[i][1]-p[j][1]);
}
inline bool is_eq(double a,double b){ // 判断浮点数相等
    return fabs(a-b) < EPS;
}

vector<int> six_vec; // 存储长度为6的相关顶点
vector<int> nine_vec; // 存储长度为9的相关顶点

int A;
int B;
int C;

void init(){
    for (int i = 1; i <= 20; i++) {
        scanf("%lf %lf",&p[i][0],&p[i][1]);
    }
    six_vec.clear();
    nine_vec.clear();
}
void find_ABC(){
    for (int i = 1; i <= 19; i++) {
        double x = get_dis(i,i+1);
        if(is_eq(x,81)){ // 9 * 9
            six_vec.push_back(i);
            six_vec.push_back(i+1);
        }else if(is_eq(x,36)){ // 6 * 6
            nine_vec.push_back(i);
            nine_vec.push_back(i+1);
        }
    }
    
    double temp = get_dis(1,20);
    if(is_eq(temp,81)){
        six_vec.push_back(1);
        six_vec.push_back(20);
    }else if(is_eq(temp,36)){
        nine_vec.push_back(1);
        nine_vec.push_back(20);
    }
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            if(six_vec[i] == nine_vec[j]){
                B = six_vec[i];
            }
        }
    }
    
    if(six_vec[0] == B){
        C = six_vec[1];
    }else{
        C = six_vec[0];
    }
    
    if(nine_vec[0] == B){
        A = nine_vec[1];
    }else{
        A = nine_vec[0];
    }
}


int main(){
    int t;
    scanf("%d",&t);
    
    while (t--) {
        /* 读入顶点坐标 */
        init();
        /* 找到长度为6的线段的有关顶点和长度为9的线段的有关顶点*/
        find_ABC();
        /* 求出向量 */
        typedef pair<double,double> VECTOR;
        VECTOR n(p[C][0]-p[A][0],p[C][1]-p[A][1]); // AC 向量
        VECTOR m(p[C][0]-p[B][0],p[C][1]-p[B][1]); // BC 向量
        
        /* 判断左右手[叉积判断向量的顺逆旋转关系] */
        int ans = n.first * m.second - n.second * m.first;
        if(ans < 0){
            puts("left");
        }else{
            puts("right");
        }
        
    }
    return 0;
}
posted @ 2020-07-18 22:37  popozyl  阅读(181)  评论(0编辑  收藏  举报