Displacement from origin after N moves of given distances in specified directions
Given an array A[] consisting of characters ‘U’, ‘D’, ‘L’, and ‘R’ representing directions up, down, left, and right, and another array B[] consisting of N positive integers, the task is to find the displacement of a robot, starting its journey from (0, 0) facing toward North, after N moves, where in the ith move, the robot moves a distance B[i] in the direction A[i].
Examples:
Input: A[] = {U, R, R, R, R}, B[] = {1, 1, 1, 1, 0}
Output: 0 North
Explanation:
Initially, the robot is at (0, 0) facing North.
After 1st move, the position is (0, 1) facing North.
After 2nd move, the position is (1, 1) facing East.
After 3rd move, the position is (1, 0) facing South.
After 4th move, the position is (0, 0) facing West.
After 5th move, the position is (0, 0) facing North.Therefore, the displacement is 0 and the final direction is North.
Input: A[] = {U, L, R, D, R}, B[] = {5, 5, 5, 5, 5}
Output: 11 West
Approach: The given problem can be solved by traversing the given array and keep the track of the direction and the distance traveled in the direction North(N), South(S), East(E), and West(W). Follow the steps below to solve the problem:
- Initialize variables, say N, S, E, and W that stores the distances traversed in the directions North, South, East, and West respectively after given set of movements.
- Initialize a variable, say P as North, that stores the final direction after performing the given set of movements.
- Traverse the given arrays A[] and B[] simultaneously and update the value of distance traverse in all the directions with the current direction.
- Now, the vertical displacement is given by (N – S) and the horizontal displacement is given by (E – S).
- After completing the above steps, print the final displacement as
and the final direction stored in P.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> 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 |
Javascript
<script> // Javascript program for the above approach // Function to find the displacement // from the origin and direction after // performing the given set of moves function finalPosition(a, b, M) { // Stores the distances travelled // in the directions North, South, // East, and West respectively let n = 0, s = 0, e = 0, w = 0; // Store the initial // position of robot let p = 'N' ; // Traverse the array B[] for (let 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 let ver_disp = n - s; // Stores the total // horizontal displacement let hor_disp = e - w; // Find the displacement let displacement = Math.ceil(Math.sqrt( (ver_disp * ver_disp) + (hor_disp * hor_disp))); // Print the displacement and // direction after N moves document.write(displacement + " " + p); } let A = [ 'U' , 'R' , 'R' , 'R' , 'R' ]; let B = [ 1, 1, 1, 1, 0 ]; let N = 1; finalPosition(A, B, N); // This code is contributed by suresh07. </script> |
1 N
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...