给定 x 和 y 坐标分别为 SX 和 SY 的起点,以及表示要遵循的方向的序列“D” ,任务是找到目的地的坐标。字符串D 由字符S、N、W 和 E 组成,其中[ S = South(向下移动一个单位),N = North(向上移动一个单位),W = West(向左移动一个单位),E = East(移动一个单位)正确的一个单元)]
例子。
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)
方法:遍历字符串D并相应地增加或减少坐标SX和SY并打印它们的最终值。
下面是上述方法的实现:
C++
// C++ implementation of
// the above approach
#include
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<
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
输出:
(3,1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live