Find the final co-ordinates reached by following a sequence of directions
Last Updated :
12 Jul, 2021
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++
#include<bits/stdc++.h>
using namespace std;
void finalCoordinates( int SX, int SY, string D){
for ( int i = 0; i < D.length(); i++){
if (D[i] == 'N' )
SY += 1;
else if (D[i] == 'S' )
SY -= 1;
else if (D[i] == 'E' )
SX += 1;
else
SX -= 1;
}
string ans = '(' + to_string(SX) + ',' +
to_string(SY) + ')' ;
cout<<ans<<endl;
}
int main(){
int SX = 2, SY = 2;
string D = "NSSE" ;
finalCoordinates(SX, SY, D);
}
|
Java
import java.util.*;
class GFG{
static void finalCoordinates( int SX, int SY,
char []D)
{
for ( int i = 0 ; i < D.length; i++)
{
if (D[i] == 'N' )
SY += 1 ;
else if (D[i] == 'S' )
SY -= 1 ;
else if (D[i] == 'E' )
SX += 1 ;
else
SX -= 1 ;
}
String ans = '(' + String.valueOf(SX) + ',' +
String.valueOf(SY) + ')' ;
System.out.print(ans);
}
public static void main(String[] args)
{
int SX = 2 , SY = 2 ;
String D = "NSSE" ;
finalCoordinates(SX, SY, D.toCharArray());
}
}
|
Python3
def finalCoordinates(SX, SY, D):
for i in range ( len (D)):
if (D[i] = = 'N' ):
SY + = 1
elif (D[i] = = 'S' ):
SY - = 1
elif (D[i] = = 'E' ):
SX + = 1
else :
SX - = 1
ans = '(' + str (SX) + ',' + str (SY) + ')'
print (ans)
if __name__ = = '__main__' :
SX, SY = 2 , 2
D = "NSSE"
finalCoordinates(SX, SY, D)
|
C#
using System;
class GFG{
static void finalCoordinates( int SX, int SY,
char []D)
{
for ( int i = 0; i < D.Length; i++)
{
if (D[i] == 'N' )
{
SY += 1;
}
else if (D[i] == 'S' )
{
SY -= 1;
}
else if (D[i] == 'E' )
{
SX += 1;
}
else
{
SX -= 1;
}
}
String ans = '(' + String.Join( "" , SX) + ',' +
String.Join( "" , SY) + ')' ;
Console.Write(ans);
}
public static void Main(String[] args)
{
int SX = 2, SY = 2;
String D = "NSSE" ;
finalCoordinates(SX, SY, D.ToCharArray());
}
}
|
Javascript
<script>
function finalCoordinates(SX,SY,D)
{
for (let i = 0; i < D.length; i++)
{
if (D[i] == 'N' )
SY += 1;
else if (D[i] == 'S' )
SY -= 1;
else if (D[i] == 'E' )
SX += 1;
else
SX -= 1;
}
let ans = '(' + (SX).toString() + ',' +
(SY).toString() + ')' ;
document.write(ans);
}
let SX = 2, SY = 2;
let D = "NSSE" ;
finalCoordinates(SX, SY, D.split( "" ));
</script>
|
Share your thoughts in the comments
Please Login to comment...