列表

详情


NC209385. OperationLove

描述

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.

输入描述

The first line contains one integer t () --- 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:

It obviously is the right palm.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 14ms, 内存消耗: 504K, 提交时间: 2020-07-18 12:56:36

#include<bits/stdc++.h>
using namespace std;
const double eps=1e-2;
int sgn(double x)
{
	if(x<-eps)return -1;
	if(x>eps)return 1;
	return 0;
}
struct Point
{
	double x,y;
	void read(){scanf("%lf%lf",&x,&y);}
	Point operator -(const Point &t)const{return {x-t.x,y-t.y};}
	double operator *(const Point &t)const{return x*t.y-y*t.x;}
	double norm(){return x*x+y*y;}
}p[30],v[30];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int x=0,y;
		for(int i=1;i<=20;i++)p[i].read();
		for(int i=1;i<=20;i++)v[i]=p[i%20+1]-p[i];
		for(int i=1;i<=20;i++)if(fabs(v[i].norm()-36)<eps)x=i;
		if(fabs(v[x%20+1].norm()-1)<eps)y=x%20+1;
		else y=(x+18)%20+1;
		printf("%s\n",sgn(v[x]*v[y])<0?"right":"left");
	}
}

C++(g++ 7.5.0) 解法, 执行用时: 18ms, 内存消耗: 716K, 提交时间: 2023-05-01 17:10:15

#include<bits/stdc++.h>
using namespace std;
struct po
{
	double x,y;
} a[30];
double dis(po x,po y) //边长
{
	return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
double cj(po x,po y,po z) //叉积
{
	return (y.x-x.x)*(z.y-x.y)-(y.y-x.y)*(z.x-y.x);
}
int main()
{
	int t,i;
	bool flag;
	const double e=1e-5;
	scanf("%d",&t);
	while(t--)
	{
		flag=0;
		for(i=0;i<20;i++)
			scanf("%lf%lf",&a[i].x,&a[i].y);
		for(i=0;i<20;i++)
			if(fabs(dis(a[i],a[(i+1)%20])-9)<e)
			{
				po x=a[i],y=a[(i+1)%20],z=a[(i+2)%20];
				if(cj(x,y,z)>0 && fabs(dis(y,z)-6)<e || cj(x,y,z)<0 && fabs(dis(y,z)-8)<e) flag=1;
				break;
			}
				
		if(flag) printf("left\n");
		else printf("right\n");
	}
    return 0;
}

Python3 解法, 执行用时: 168ms, 内存消耗: 4816K, 提交时间: 2023-05-01 14:21:00

def dis(a,b):
    return (a[0]-b[0])**2+(a[1]-b[1])**2

for _ in range(int(input())):
    l=[list(map(float,input().split())) for _ in range(20)]
    x=y=0
    for z in l:
        x+=z[0]
        y+=z[1]
    
    x/=20
    y/=20

    l+=l
    for i in range(10,30):
        d=dis(l[i],l[i+1])
        if 80<d:
            a=(l[i][0]+l[i+1][0])/2
            b=(l[i][1]+l[i+1][1])/2
            
            if dis(l[i-1],l[i])>dis(l[i+1],l[i+2]):
                c,d=l[i-1]
            else:
                c,d=l[i+2]
                        
            x-=a
            y-=b
            c-=a
            d-=b
            
            print('right' if x*d<y*c else 'left')

C++11(clang++ 3.9) 解法, 执行用时: 14ms, 内存消耗: 376K, 提交时间: 2020-07-18 13:02:16

#include <cmath>
#include <cstdio>
#include <algorithm>

double x[25],y[25];
int T,n;

double dis(int i,int j){return std::sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));}

bool same(double a,double b){return std::fabs(a-b)<=1e-5;}

double cross(double x,double y,double _x,double _y){return x*_y-y*_x;}

int main(){
	scanf("%d",&T);
	while(T--){
		n=20;
		for(int i=1;i<=n;i++)scanf("%lf%lf",&x[i],&y[i]);
		for(int i=1;i<=n;i++){
			int j=i%n+1,k=j%n+1;
			if(same(dis(i,j),9)){
				bool ans=same(dis(j,k),8)^(cross(x[i]-x[j],y[i]-y[j],x[k]-x[j],y[k]-y[j])<0);
				printf("%s\n",ans?"left":"right");
				break;
			}
		}
	}
}

上一题