📜  在指定方向上给定距离N次移动后的原点位移

📅  最后修改于: 2021-04-17 14:11:00             🧑  作者: Mango

给定一个由字符‘U’‘D’‘L’‘R’组成的数组A []表示向上向下向左向右的方向,以及另一个由N个正整数组成的数组B [] ,该任务是要找到一个机器人的位移,从朝向(0,0)开始其旅程,N移动之后,其中在i移动时,机器人移动的距离B [i]于方向A [i]中

例子:

方法:可以通过遍历给定数组并跟踪方向和在North(N)South(S)East(E)West(W)方向上行进的距离来解决给定的问题。请按照以下步骤解决问题:

  • 初始化变量,例如NSEW ,这些变量存储在给定的运动之后分别沿西方向移动的距离。
  • 初始化变量,例如PNorth ,在执行给定的一组运动之后,该变量存储最终方向。
  • 同时遍历给定数组A []B [] ,并使用当前方向更新所有方向上的距离遍历值。
  • 现在,垂直位移由(N – S)给出,水平位移由(E – S)给出
  • 完成上述步骤后,将最终位移打印为\sqrt((N - S)^2 + (E - W)^2)         最终方向存储在P中

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the displacement
// from the origin and direction after
// performing the given set of moves
void finalPosition(char a[], int b[],
                   int M)
{
    // Stores the distances travelled
    // in the directions North, South,
    // East, and West respectively
    int n = 0, s = 0, e = 0, w = 0;
 
    // Store the initial
    // position of robot
    char p = 'N';
 
    // Traverse the array B[]
    for (int i = 0; i < M; i++) {
 
        // If the current
        // direction is North
        if (p == 'N') {
 
            if (a[i] == 'U') {
                p = 'N';
                n = n + b[i];
            }
            else if (a[i] == 'D') {
                p = 'S';
                s = s + b[i];
            }
            else if (a[i] == 'R') {
                p = 'E';
                e = e + b[i];
            }
            else if (a[i] == 'L') {
                p = 'W';
                w = w + b[i];
            }
        }
 
        // If the current
        // direction is South
        else if (p == 'S') {
            if (a[i] == 'U') {
                p = 'S';
                s = s + b[i];
            }
            else if (a[i] == 'D') {
                p = 'N';
                n = n + b[i];
            }
            else if (a[i] == 'R') {
                p = 'W';
                w = w + b[i];
            }
            else if (a[i] == 'L') {
                p = 'E';
                e = e + b[i];
            }
        }
 
        // If the current
        // direction is East
        else if (p == 'E') {
            if (a[i] == 'U') {
                p = 'E';
                e = e + b[i];
            }
            else if (a[i] == 'D') {
                p = 'W';
                w = w + b[i];
            }
            else if (a[i] == 'R') {
                p = 'S';
                s = s + b[i];
            }
            else if (a[i] == 'L') {
                p = 'N';
                n = n + b[i];
            }
        }
 
        // If the current
        // direction is West
        else if (p == 'W') {
            if (a[i] == 'U') {
                p = 'W';
                w = w + b[i];
            }
            else if (a[i] == 'D') {
                p = 'E';
                e = e + b[i];
            }
            else if (a[i] == 'R') {
                p = 'N';
                n = n + b[i];
            }
            else if (a[i] == 'L') {
                p = 'S';
                s = s + b[i];
            }
        }
    }
 
    // Stores the total
    // vertical displacement
    int ver_disp = n - s;
 
    // Stores the total
    // horizontal displacement
    int hor_disp = e - w;
 
    // Find the displacement
    int displacement = floor(
        sqrt((ver_disp * ver_disp)
             + (hor_disp * hor_disp)));
 
    // Print the displacement and
    // direction after N moves
    cout << displacement << " " << p;
}
 
// Driver Code
int main()
{
    char A[] = { 'U', 'R', 'R', 'R', 'R' };
    int B[] = { 1, 1, 1, 1, 0 };
  
    int N = sizeof(A) / sizeof(B[0]);
 
    finalPosition(A, B, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the displacement
// from the origin and direction after
// performing the given set of moves
static void finalPosition(char[] a, int[] b, int M)
{
     
    // Stores the distances travelled
    // in the directions North, South,
    // East, and West respectively
    int n = 0, s = 0, e = 0, w = 0;
 
    // Store the initial
    // position of robot
    char p = 'N';
 
    // Traverse the array B[]
    for(int i = 0; i < M; i++)
    {
         
        // If the current
        // direction is North
        if (p == 'N')
        {
            if (a[i] == 'U')
            {
                p = 'N';
                n = n + b[i];
            }
            else if (a[i] == 'D')
            {
                p = 'S';
                s = s + b[i];
            }
            else if (a[i] == 'R')
            {
                p = 'E';
                e = e + b[i];
            }
            else if (a[i] == 'L')
            {
                p = 'W';
                w = w + b[i];
            }
        }
 
        // If the current
        // direction is South
        else if (p == 'S')
        {
            if (a[i] == 'U')
            {
                p = 'S';
                s = s + b[i];
            }
            else if (a[i] == 'D')
            {
                p = 'N';
                n = n + b[i];
            }
            else if (a[i] == 'R')
            {
                p = 'W';
                w = w + b[i];
            }
            else if (a[i] == 'L')
            {
                p = 'E';
                e = e + b[i];
            }
        }
 
        // If the current
        // direction is East
        else if (p == 'E')
        {
            if (a[i] == 'U')
            {
                p = 'E';
                e = e + b[i];
            }
            else if (a[i] == 'D')
            {
                p = 'W';
                w = w + b[i];
            }
            else if (a[i] == 'R')
            {
                p = 'S';
                s = s + b[i];
            }
            else if (a[i] == 'L')
            {
                p = 'N';
                n = n + b[i];
            }
        }
 
        // If the current
        // direction is West
        else if (p == 'W')
        {
            if (a[i] == 'U')
            {
                p = 'W';
                w = w + b[i];
            }
            else if (a[i] == 'D')
            {
                p = 'E';
                e = e + b[i];
            }
            else if (a[i] == 'R')
            {
                p = 'N';
                n = n + b[i];
            }
            else if (a[i] == 'L')
            {
                p = 'S';
                s = s + b[i];
            }
        }
    }
 
    // Stores the total
    // vertical displacement
    int ver_disp = n - s;
 
    // Stores the total
    // horizontal displacement
    int hor_disp = e - w;
 
    // Find the displacement
    int displacement = (int)Math.ceil(Math.sqrt(
        (ver_disp * ver_disp) + (hor_disp * hor_disp)));
 
    // Print the displacement and
    // direction after N moves
    System.out.print(displacement + " " + p);
}
 
// Driver Code
public static void main(String args[])
{
    char[] A = { 'U', 'R', 'R', 'R', 'R' };
    int[] B = { 1, 1, 1, 1, 0 };
    int N = 1;
     
    finalPosition(A, B, N);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
from math import sqrt, floor
 
# Function to find the displacement
# from the origin and direction after
# performing the given set of moves
def finalPosition(a, b, M):
     
    # Stores the distances travelled
    # in the directions North, South,
    # East, and West respectively
    n = 0
    s = 0
    e = 0
    w = 0
 
    # Store the initial
    # position of robot
    p = 'N'
 
    # Traverse the array B[]
    for i in range(M):
         
        # If the current
        # direction is North
        if (p == 'N'):
            if (a[i] == 'U'):
                p = 'N'
                n = n + b[i]
            elif (a[i] == 'D'):
                p = 'S'
                s = s + b[i]
            elif (a[i] == 'R'):
                p = 'E'
                e = e + b[i]
            elif (a[i] == 'L'):
                p = 'W'
                w = w + b[i]
 
        # If the current
        # direction is South
        elif (p == 'S'):
            if (a[i] == 'U'):
                p = 'S'
                s = s + b[i]
            elif(a[i] == 'D'):
                p = 'N'
                n = n + b[i]
            elif(a[i] == 'R'):
                p = 'W'
                w = w + b[i]
            elif(a[i] == 'L'):
                p = 'E'
                e = e + b[i]
 
        # If the current
        # direction is East
        elif(p == 'E'):
            if (a[i] == 'U'):
                p = 'E'
                e = e + b[i]
            elif (a[i] == 'D'):
                p = 'W'
                w = w + b[i]
            elif (a[i] == 'R'):
                p = 'S'
                s = s + b[i]
            elif (a[i] == 'L'):
                p = 'N'
                n = n + b[i]
 
        # If the current
        # direction is West
        elif (p == 'W'):
            if (a[i] == 'U'):
                p = 'W'
                w = w + b[i]
            elif (a[i] == 'D'):
                p = 'E'
                e = e + b[i]
            elif (a[i] == 'R'):
                p = 'N'
                n = n + b[i]
            elif (a[i] == 'L'):
                p = 'S'
                s = s + b[i]
 
    # Stores the total
    # vertical displacement
    ver_disp = n - s
 
    # Stores the total
    # horizontal displacement
    hor_disp = e - w
 
    # Find the displacement
    displacement = floor(sqrt((ver_disp * ver_disp) +
                              (hor_disp * hor_disp)) + 1)
 
    # Print the displacement and
    # direction after N moves
    print(displacement,p)
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 'U', 'R', 'R', 'R', 'R' ]
    B = [ 1, 1, 1, 1, 0 ]
    N = len(A)
     
    finalPosition(A, B, N)
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the displacement
// from the origin and direction after
// performing the given set of moves
static void finalPosition(char[] a, int[] b, int M)
{
     
    // Stores the distances travelled
    // in the directions North, South,
    // East, and West respectively
    int n = 0, s = 0, e = 0, w = 0;
 
    // Store the initial
    // position of robot
    char p = 'N';
 
    // Traverse the array B[]
    for(int i = 0; i < M; i++)
    {
         
        // If the current
        // direction is North
        if (p == 'N')
        {
            if (a[i] == 'U')
            {
                p = 'N';
                n = n + b[i];
            }
            else if (a[i] == 'D')
            {
                p = 'S';
                s = s + b[i];
            }
            else if (a[i] == 'R')
            {
                p = 'E';
                e = e + b[i];
            }
            else if (a[i] == 'L')
            {
                p = 'W';
                w = w + b[i];
            }
        }
 
        // If the current
        // direction is South
        else if (p == 'S')
        {
            if (a[i] == 'U')
            {
                p = 'S';
                s = s + b[i];
            }
            else if (a[i] == 'D')
            {
                p = 'N';
                n = n + b[i];
            }
            else if (a[i] == 'R')
            {
                p = 'W';
                w = w + b[i];
            }
            else if (a[i] == 'L')
            {
                p = 'E';
                e = e + b[i];
            }
        }
 
        // If the current
        // direction is East
        else if (p == 'E')
        {
            if (a[i] == 'U')
            {
                p = 'E';
                e = e + b[i];
            }
            else if (a[i] == 'D')
            {
                p = 'W';
                w = w + b[i];
            }
            else if (a[i] == 'R')
            {
                p = 'S';
                s = s + b[i];
            }
            else if (a[i] == 'L')
            {
                p = 'N';
                n = n + b[i];
            }
        }
 
        // If the current
        // direction is West
        else if (p == 'W')
        {
            if (a[i] == 'U')
            {
                p = 'W';
                w = w + b[i];
            }
            else if (a[i] == 'D')
            {
                p = 'E';
                e = e + b[i];
            }
            else if (a[i] == 'R')
            {
                p = 'N';
                n = n + b[i];
            }
            else if (a[i] == 'L')
            {
                p = 'S';
                s = s + b[i];
            }
        }
    }
 
    // Stores the total
    // vertical displacement
    int ver_disp = n - s;
 
    // Stores the total
    // horizontal displacement
    int hor_disp = e - w;
 
    // Find the displacement
    int displacement = (int)Math.Ceiling(Math.Sqrt(
        (ver_disp * ver_disp) + (hor_disp * hor_disp)));
 
    // Print the displacement and
    // direction after N moves
    Console.WriteLine(displacement + " " + p);
}
 
// Driver Code
public static void Main()
{
    char[] A = { 'U', 'R', 'R', 'R', 'R' };
    int[] B = { 1, 1, 1, 1, 0 };
    int N = 1;
     
    finalPosition(A, B, N);
}
}
 
// This code is contributed by ukasp


输出:
1 N

时间复杂度: O(N)
辅助空间: O(1)