Find the final co-ordinates reached by following a sequence of directions
Given a starting point with x and y co-ordinates SX and SY respectively, and a sequence ‘D’ denoting the directions to be followed, the task is to find the co-ordinate of the destination. The string D consists of characters S,N,W and E where [ S = South (move down one unit), N = North (move up one unit), W = West (move left one unit), E = East (move right one unit) ]
Examples .
Input: SX = 2, SY = 3, D = “SEWW”
Output: (1,2)
Explanation: From the given point, it will be directed
to S(2, 2)->E(3, 2)->W(2, 2)->W(1, 2). Therefore the final position is (1,2)
Input: SX = 2, SY = 2, D = “NSSE”
Output: (3,1)
Approach: Traverse through the string D and increment or decrement the co-ordinates SX and SY accordingly and print their final values.
Below is the implementation of the above approach:
C++
// C++ implementation of// the above approach#include<bits/stdc++.h>using namespace std;// Function to print the final position// of the point after traversing through// the given directionsvoid finalCoordinates(int SX, int SY, string D){ // Traversing through the given directions for (int i = 0; i < D.length(); i++){ // If its north or south the point // will move left or right if (D[i] == 'N') SY += 1; else if(D[i] == 'S') SY -= 1; // If its west or east the point // will move upwards or downwards else if(D[i] == 'E') SX += 1; else SX -= 1; } // Returning the final position string ans = '(' + to_string(SX) + ',' + to_string(SY) + ')'; cout<<ans<<endl;} // Driver Codeint main(){ int SX = 2, SY = 2; string D = "NSSE"; finalCoordinates(SX, SY, D);}// This code is contributed by Samarth |
Java
// Java implementation of the above approachimport java.util.*;class GFG{// Function to print the final position// of the point after traversing through// the given directionsstatic void finalCoordinates(int SX, int SY, char []D){ // Traversing through the given directions for(int i = 0; i < D.length; i++) { // If its north or south the point // will move left or right if (D[i] == 'N') SY += 1; else if(D[i] == 'S') SY -= 1; // If its west or east the point // will move upwards or downwards else if(D[i] == 'E') SX += 1; else SX -= 1; } // Returning the final position String ans = '(' + String.valueOf(SX) + ',' + String.valueOf(SY) + ')'; System.out.print(ans);} // Driver Codepublic static void main(String[] args){ int SX = 2, SY = 2; String D = "NSSE"; finalCoordinates(SX, SY, D.toCharArray());}}// This code is contributed by gauravrajput1 |
Python3
# Python3 implementation of# the above approach# Function to print the final position# of the point after traversing through# the given directionsdef finalCoordinates(SX, SY, D): # Traversing through the given directions for i in range (len(D)): # If its north or south the point # will move left or right if (D[i] == 'N'): SY += 1 elif (D[i] == 'S'): SY -= 1 # If its west or east the point # will move upwards or downwards elif (D[i] == 'E'): SX += 1 else : SX -= 1 # Returning the final position ans = '(' + str(SX) + ',' + str(SY) + ')' print (ans) # Driver Codeif __name__ == '__main__': SX, SY = 2,2 D = "NSSE" finalCoordinates(SX, SY, D) # This code is contributed by parna_28 |
C#
// C# implementation of the above approachusing System;class GFG{// Function to print the readonly position// of the point after traversing through// the given directionsstatic void finalCoordinates(int SX, int SY, char []D){ // Traversing through the given directions for(int i = 0; i < D.Length; i++) { // If its north or south the point // will move left or right if (D[i] == 'N') { SY += 1; } else if(D[i] == 'S') { SY -= 1; } // If its west or east the point // will move upwards or downwards else if(D[i] == 'E') { SX += 1; } else { SX -= 1; } } // Returning the readonly position String ans = '(' + String.Join("", SX) + ',' + String.Join("", SY) + ')'; Console.Write(ans);} // Driver Codepublic static void Main(String[] args){ int SX = 2, SY = 2; String D = "NSSE"; finalCoordinates(SX, SY, D.ToCharArray());}}// This code is contributed by gauravrajput1 |
Javascript
<script>// JavaScript implementation of the above approach// Function to print the final position// of the point after traversing through// the given directionsfunction finalCoordinates(SX,SY,D){ // Traversing through the given directions for(let i = 0; i < D.length; i++) { // If its north or south the point // will move left or right if (D[i] == 'N') SY += 1; else if(D[i] == 'S') SY -= 1; // If its west or east the point // will move upwards or downwards else if(D[i] == 'E') SX += 1; else SX -= 1; } // Returning the final position let ans = '(' + (SX).toString() + ',' + (SY).toString() + ')'; document.write(ans);}// Driver Codelet SX = 2, SY = 2;let D = "NSSE";finalCoordinates(SX, SY, D.split(""));// This code is contributed by unknown2108</script> |
(3,1)



