📜  通过遵循一系列指示找到最终到达的坐标

📅  最后修改于: 2021-06-25 19:22:10             🧑  作者: Mango

给定一个分别具有x和y坐标SX和SY的起点,并指定表示要遵循的方向的序列“ D” ,任务是找到目的地的坐标。字符串D由字符S,N,W和E组成,其中[S =南(向下移动一个单位),N =北(向上移动一个单位),W =西(向左移动一个单位),E =东(向左移动一个单位)右一个单元)]

例子

方法:遍历字符串D并相应地增加或减少坐标SXSY并打印其最终值。

下面是上述方法的实现:

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现场课程》和《 Geeks现场课程美国》。