📜  使用顺时针或逆时针运动到达点

📅  最后修改于: 2021-04-29 03:52:53             🧑  作者: Mango

给定开始和结束位置以及数字N。鉴于我们只能在四个方向上移动,如下图所示。移动方向为U ( \uparrow  ), R \rightarrow  D \downarrow  L \leftarrow  。我们需要编写一个程序,以确定是否从给定的起始位置开始,我们可以在移动任何方向(顺时针或逆时针)在到达确切的N移动给定的端部位置。

例子 :

Input: start = U , end = L , N = 3 
Output: Clockwise 
Explanation: Step 1: move clockwise to reach R
             Step 2: move clockwise to reach D
             Step 3: move clockwise to reach L 
So we reach from U to L in 3 steps moving in 
clockwise direction.

Input: start = R , end = L , N = 3
Output: Not possible 
Explanation: It is not possible to start from 
R and end at L in 3 steps moving about in any 
direction. 

Input: start = D , end = R , N = 7 
Output: Clockwise
Explanation: Starting at D, we complete one 
complete clockwise round in 4 steps to reach D 
again, then it takes 3 step to reach R 

解决此问题的想法是观察到,我们可以沿任何方向(顺时针或逆时针)行驶,以4步完成一个回合,因此,执行n%4步等效于从起点开始执行n步。因此, n减小为n%4 。将“ U”的值视为0,将“ R”的值视为1,将“ D”的值视为2,将“ L”的值视为3 。如果abs(value(a)-value(b))为2并且n也为2,那么我们可以沿顺时针方向或逆时针方向移动以从起始位置到达终点位置。如果沿顺时针方向移动k步将我们从起始位置移至终点,则可以说顺时针移动的条件为(value(a)+ k)%4 == value(b) 。同样,逆时针移动的条件为(value(a)+ k * 3)%4 == value(b),因为从位置a沿顺时针方向走k步等效于(a + k * 3)%4沿逆时针方向移动。
下面是上述方法的实现:

C++
// CPP program to determine if
// starting from the starting
// position we can reach the 
// end position in N moves
// moving about any direction
#include 
using namespace std;
 
// function that returns mark
// up value of directions
int value(char a)
{
    if (a == 'U')
        return 0;
    if (a == 'R')
        return 1;
    if (a == 'D')
        return 2;
    if (a == 'L')
        return 3;
}
 
// function to print
// the possible move
void printMove(char a, char b, int n)
{
    // mod with 4 as completing
    // 4 setps means completing
    // one single round
    n = n % 4;
 
    // when n is 2 and the
    // difference between moves is 2
    if (n == 2 and abs(value(a) -
                       value(b)) == 2)
        cout << "Clockwise or Anticlockwise";
 
    // anticlockwise condition
    else if ((value(a) + n * 3) % 4 == value(b))
        cout << "Anticlockwise";
 
    // clockwise condition
    else if ((value(a) + n) % 4 == value(b))
        cout << "Clockwise";
    else
        cout << "Not Possible";
}
 
// Driver Code
int main()
{
    char a = 'D', b = 'R';
    int n = 7;
    printMove(a, b, n);
 
    return 0;
}


Java
// Java program to determine if
// starting from the starting
// position we can reach the
// end position in N moves
// moving about any direction
class GFG
{
    // function that returns mark
    // up value of directions
    static int value(char a)
    {
        if (a == 'U')
            return 0;
        if (a == 'R')
            return 1;
        if (a == 'D')
            return 2;
        if (a == 'L')
            return 3;
             
            return -1;
    }
 
    // function to print
    // the possible move
    static void printMove(char a,
                          char b,
                          int n)
    {
        // mod with 4 as completing
        // 4 setps means completing
        // one single round
        n = n % 4;
     
        // when n is 2 and
        // the difference
        // between moves is 2
        if (n == 2 && Math.abs(value(a) -
                               value(b)) == 2)
            System.out.println("Clockwise " +
                        " or Anticlockwise");
     
        // anticlockwise condition
        else if ((value(a) + n * 3) %
                       4 == value(b))
            System.out.println("Anticlockwise");
     
        // clockwise condition
        else if ((value(a) + n) % 4 == value(b))
            System.out.println("Clockwise");
        else
            System.out.println("Not Possible");
    }
 
    // Driver Code
    public static void main(String args[])
    {
        char a = 'D', b = 'R';
        int n = 7;
        printMove(a, b, n);
    }
}
 
// This code is contributed by Sam007


Python3
# python program to determine
# if starting from the starting
# position we can reach the end
# position in N moves moving 
# any direction
 
# function that returns mark
# up value of directions
def value(a):
     
    if (a == 'U'):
        return 0
    if (a == 'R'):
        return 1
    if (a == 'D'):
        return 2
    if (a == 'L'):
        return 3
 
# function to print
# the possible move
def printMove(a, b, n):
     
    # mod with 4 as completing
    # 4 setps means completing
    # one single round
    n = n % 4;
 
    # when n is 2 and
    # the difference
    # between moves is 2
    if (n == 2 and
        abs(value(a) - value(b)) == 2):
        print ("Clockwise or Anticlockwise")
 
    # anticlockwise condition
    elif ((value(a) + n * 3) % 4 == value(b)):
        print ("Anticlockwise")
 
    # clockwise condition
    elif ((value(a) + n) % 4 == value(b)):
        print ("Clockwise")
    else:
        print ("Not Possible")
 
 
# Driver Code
a = 'D'
b = 'R'
n = 7
printMove(a, b, n)
 
# This code is contributed by Sam007.


C#
// C# program to determine
// if starting from the
// starting position we
// can reach the end position
// in N moves moving about
// any direction
using System;
 
class GFG
{
    // function that returns mark
    // up value of directions
    static int value(char a)
    {
        if (a == 'U')
            return 0;
        if (a == 'R')
            return 1;
        if (a == 'D')
            return 2;
        if (a == 'L')
            return 3;
         
            return -1;
    }
 
    // function to print
    // the possible move
    static void printMove(char a,
                          char b,
                          int n)
    {
        // mod with 4 as completing
        // 4 setps means completing
        // one single round
        n = n % 4;
     
        // when n is 2 and
        // the difference
        // between moves is 2
        if (n == 2 && Math.Abs(value(a) -
                               value(b)) == 2)
            Console.Write("Clockwise " +
                    "or Anticlockwise");
     
        // anticlockwise condition
        else if ((value(a) + n * 3) %
                        4 == value(b))
            Console.Write("Anticlockwise");
     
        // clockwise condition
        else if ((value(a) + n) %
                    4 == value(b))
            Console.WriteLine("Clockwise");
        else
            Console.WriteLine("Not Possible");
    }
 
    // Driver Code
    public static void Main()
    {
    char a = 'D', b = 'R';
    int n = 7;
    printMove(a, b, n);
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出 :

Clockwise