Codeforces 227A Where do I Turn?(代数)

本文解析了CodeForces平台上的一道A级题目——《WheredoITurn?》。该题通过三个点A、B、C的位置信息,判断从A到B再到C的过程中英雄应如何转向(左转、直行或右转)。文章提供了详细的算法实现过程,包括向量运算和角度计算等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


A. Where do I Turn?
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Trouble came from the overseas lands: a three-headed dragon Gorynych arrived. The dragon settled at point C and began to terrorize the residents of the surrounding villages.

A brave hero decided to put an end to the dragon. He moved from point A to fight with Gorynych. The hero rode from point A along a straight road and met point B on his way. The hero knows that in this land for every pair of roads it is true that they are either parallel to each other, or lie on a straight line, or are perpendicular to each other. He also knows well that points B and C are connected by a road. So the hero must either turn 90 degrees to the left or continue riding straight ahead or turn 90 degrees to the right. But he forgot where the point C is located.

Fortunately, a Brave Falcon flew right by. It can see all three points from the sky. The hero asked him what way to go to get to the dragon's lair.

If you have not got it, you are the falcon. Help the hero and tell him how to get him to point C: turn left, go straight or turn right.

At this moment the hero is believed to stand at point B, turning his back to point A.

Input

The first input line contains two space-separated integers xa, ya (|xa|, |ya| ≤ 109) — the coordinates of point A. The second line contains the coordinates of point B in the same form, the third line contains the coordinates of point C.

It is guaranteed that all points are pairwise different. It is also guaranteed that either point B lies on segment AC, or angle ABC is right.

Output

Print a single line. If a hero must turn left, print "LEFT" (without the quotes); If he must go straight ahead, print "TOWARDS" (without the quotes); if he should turn right, print "RIGHT" (without the quotes).

Sample test(s)
input
0 0
0 1
1 1
output
RIGHT
input
-1 -1
-3 -3
-4 -4
output
TOWARDS
input
-4 -6
-3 -7
-2 -6
output
LEFT
Note

The picture to the first sample:

The red color shows points A, B and C. The blue arrow shows the hero's direction. The green color shows the hero's trajectory.

The picture to the second sample:




给出A,B,C三点坐标,求A到B到C如何转弯(LEFT/RIGHT/TOWARDS)
无脑化为代数题,把向量AB和向量BC化为单位方向向量,求AB旋转到BC的角度,即求出左乘的旋转矩阵,用二阶行列式解一个一元二次方程,记得要用正弦值(为什么?问高中数学老师去orz)

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<time.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#define sqr(x) (x)*(x)
#define INF 0x1f1f1f1f
#define PI 3.1415926535
#define mm 

using namespace std;

double dd(double a,double b,double c,double d)
{
    return a*d-b*c;
}

int main()
{
    double xa,ya,xb,yb,xc,yc;
    cin>>xa>>ya>>xb>>yb>>xc>>yc;
    double l1=sqrt(sqr(xb-xa)+sqr(yb-ya));//求模长 
    double l2=sqrt(sqr(xc-xb)+sqr(yc-yb));
    double a=(xb-xa)/l1,b=(yb-ya)/l1,c=(xc-xb)/l2,d=(yc-yb)/l2;//单位方向向量(a,b),(c,d) 
    double D=dd(a,-b,b,a);
    double Dcos=dd(c,-b,d,a);
    double Dsin=dd(a,c,b,d);
    double cos=Dcos/D;
    double sin=Dsin/D;
    double x=asin(sin);
    
	/*  
	printf("D    %f\n",D);
    printf("Dsin %f\n",Dsin);
    printf("Dcos %f\n",Dcos);
    printf("%lf   %lf\n%lf   %lf\n",a,b,c,d);
    printf("sin %lf\n",sin);
    cout<<x/PI*180<<' '<<x<<endl;
	*/
	
    if (fabs(x)<1e-6)
        cout<<"TOWARDS"<<endl;
    else
    {   
        if (fabs(x-PI/2)<1e-6)
            cout<<"LEFT"<<endl;
        else
            cout<<"RIGHT"<<endl;
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值