Dado un punto de partida con las coordenadas x e y SX y SY respectivamente, y una secuencia ‘D’ que denota las direcciones a seguir, la tarea es encontrar la coordenada del destino. La string D consta de los caracteres S, N, W y E donde [ S = Sur (bajar una unidad), N = Norte (subir una unidad), W = Oeste (bajar una unidad a la izquierda), E = Este (bajar una unidad) una unidad a la derecha) ]
Ejemplos .
Entrada: SX = 2, SY = 3, D = “SEWW”
Salida: (1,2)
Explicación: Desde el punto dado, será dirigido
a S(2, 2)->E(3, 2)-> W(2, 2)->W(1, 2). Por lo tanto la posición final es (1,2)
Entrada: SX = 2, SY = 2, D = “NSSE”
Salida: (3,1)
Aproximación: Atraviese la string D e incremente o disminuya las coordenadas SX y SY en consecuencia e imprima sus valores finales.
A continuación se muestra la implementación del enfoque anterior:
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 directions void 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 Code int 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 approach import java.util.*; class GFG{ // Function to print the final position // of the point after traversing through // the given directions static 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 Code public 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 directions def 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 Code if __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 approach using System; class GFG{ // Function to print the readonly position // of the point after traversing through // the given directions static 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 Code public 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 directions function 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 Code let SX = 2, SY = 2; let D = "NSSE"; finalCoordinates(SX, SY, D.split("")); // This code is contributed by unknown2108 </script>
(3,1)