CodeForces - 227A:Where do I Turn?(思维题)

一位英雄在寻找三头龙Gorynych的巢穴时,需要决定从B点转向何方才能到达C点。根据B点相对于A点的位置以及C点可能的方向,任务是确定英雄应该向左转、直行还是向右转。

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

Discription
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).

Examples
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点,问需要从B点怎样走才能到到C点。在B点时背对A方向。

思路
我的思路很简单,就是把所有的情况都列出来,直走的情况直接用斜率判断,特判分母为0就可以,需要转弯的是一共14种情况,但这样子写代码会很慢,而且代码很长。
大佬用的是斜率判断,判断k1与k2的关系,进而确定向左还是向右转。

AC代码
我的无脑代码:

#include<bits/stdc++.h>
using namespace std;
int ax,ay,bx,by,cx,cy;
int main()
{
    cin>>ax>>ay>>bx>>by>>cx>>cy;
    if(bx!=ax&&cx!=bx)
    {
        int k1=(by-ay)/(bx-ax);
        int k2=(cy-by)/(cx-bx);
        if(k1==k2)
        {
            cout<<"TOWARDS"<<endl;
            return 0;
        }
    }
    else if((ax==bx&&bx==cx)||(ay==by&&by==cy))
    {
        cout<<"TOWARDS"<<endl;
        return 0;
    }
    if(ax==bx)
    {
        if((by>ay&&cx>bx)||by<ay&&cx<bx)
            cout<<"RIGHT"<<endl;
        else
            cout<<"LEFT"<<endl;
    }
    else if(by==ay)
    {
        if((bx>ax&&cy>by)||(bx<ax&&cy<by))
            cout<<"LEFT"<<endl;
        else
            cout<<"RIGHT"<<endl;
    }
    else if(bx>ax&&by>ay)
    {
        if(cx<bx&&cy>by)
            cout<<"LEFT"<<endl;
        else if(cx>bx&&cy<by)
            cout<<"RIGHT"<<endl;
    }
    else if(bx>ax&&by<ay)
    {
        if(cx>bx&&cy>by)
            cout<<"LEFT"<<endl;
        else if(cx<bx&&cy<by)
            cout<<"RIGHT"<<endl;
    }
    else if(bx<ax&&by>ay)
    {
        if(cx<bx&&cy<by)
            cout<<"LEFT"<<endl;
        else if(cx>bx&&cy>by)
            cout<<"RIGHT"<<endl;
    }
    else if(bx<ax&&by<ay)
    {
        if(cy<by&&cx>bx)
            cout<<"LEFT"<<endl;
        else if(cx<bx&&cy>by)
            cout<<"RIGHT"<<endl;
    }
    return 0;
}

大佬的精简代码:

#include<iostream>
using namespace std;
int main()
{
    int x1,y1,x2,y2,x3,y3;
    long long c1,l1,c2,l2;
    cin>>x1>>y1;
    cin>>x2>>y2;
    cin>>x3>>y3;
    c1=x2-x1,l1=y2-y1;
    c2=x3-x1,l2=y3-y1;
    if(c1*l2==c2*l1)
        cout<<"TOWARDS"<<endl;
    else if(c1*l2<c2*l1)
        cout<<"RIGHT"<<endl;
    else
        cout<<"LEFT"<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值