📜  给定动作后机器人的位置

📅  最后修改于: 2022-05-13 01:57:06.664000             🧑  作者: Mango

给定动作后机器人的位置

给定一个只能在四个方向上移动的机器人,UP(U),DOWN(D),LEFT(L),RIGHT(R)。给定一个包含移动指令的字符串。执行指令后输出机器人的坐标。机器人的初始位置在原点(0, 0)。

例子:

资料来源:高盛采访经历 |设置 36 。

方法:
分别计算向上移动(U)、向下移动(D)、向左移动(L)和向右移动(R)的次数,分别为countUp、countDown、countLeft和countRight。最终的 x 坐标将是
(countRight – countLeft) 和 y 坐标将是 (countUp – countDown)。

下面是上述思想的实现:

C++
// C++ implementation to find  final position of
// robot after the complete movement
#include 
using namespace std;
 
// Function to find  final position of
// robot after the complete movement
void finalPosition(string move)
{
    int l = move.size();
    int countUp = 0, countDown = 0;
    int countLeft = 0, countRight = 0;
 
    // Traverse the instruction string 'move'
    for (int i = 0; i < l; i++)
    {
        // For each movement increment its
        // respective counter
        if (move[i] == 'U')
            countUp++;
        else if (move[i] == 'D')
            countDown++;
        else if (move[i] == 'L')
            countLeft++;
        else if (move[i] == 'R')
            countRight++;
    }
 
    // Required final position of robot
    cout << "Final Position: ("
         << (countRight - countLeft)
         << ", " << (countUp - countDown)
         << ")" << endl;
}
 
// Driver code
int main()
{
    string move = "UDDLLRUUUDUURUDDUULLDRRRR";
    finalPosition(move);
    return 0;
}


Java
// Java implementation to find final position
// of robot after the complete movement
 
import java.io.*;
 
class GFG {
 
    // function to find final position of
    // robot after the complete movement
    static void finalPosition(String move)
    {
 
        int l = move.length();
        int countUp = 0, countDown = 0;
        int countLeft = 0, countRight = 0;
 
        // traverse the instruction string
        // 'move'
        for (int i = 0; i < l; i++)
        {
            // for each movement increment
            // its respective counter
            if (move.charAt(i) == 'U')
                countUp++;
 
            else if (move.charAt(i) == 'D')
                countDown++;
 
            else if (move.charAt(i) == 'L')
                countLeft++;
 
            else if (move.charAt(i) == 'R')
                countRight++;
        }
 
        // required final position of robot
        System.out.println("Final Position: ("
                           + (countRight - countLeft) + ", "
                           + (countUp - countDown) + ")");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String move = "UDDLLRUUUDUURUDDUULLDRRRR";
        finalPosition(move);
    }
}
 
// This code is contributed by vt_m


Python3
# Python3 implementation to find final position
# of robot after the complete movement
 
# function to find final position of
# robot after the complete movement
 
 
def finalPosition(move):
 
    l = len(move)
    countUp, countDown = 0, 0
    countLeft, countRight = 0, 0
 
    # traverse the instruction string
    # 'move'
    for i in range(l):
 
        # for each movement increment
        # its respective counter
        if (move[i] == 'U'):
            countUp += 1
 
        elif(move[i] == 'D'):
            countDown += 1
 
        elif(move[i] == 'L'):
            countLeft += 1
 
        elif(move[i] == 'R'):
            countRight += 1
 
    # required final position of robot
    print("Final Position: (", (countRight - countLeft),
          ", ", (countUp - countDown), ")")
 
 
# Driver code
if __name__ == '__main__':
    move = "UDDLLRUUUDUURUDDUULLDRRRR"
    finalPosition(move)
 
# This code is contributed by 29AjayKumar


C#
// C# implementation to find final position
// of robot after the complete movement
using System;
 
class GFG {
 
    // function to find final position of
    // robot after the complete movement
    static void finalPosition(String move)
    {
        int l = move.Length;
        int countUp = 0, countDown = 0;
        int countLeft = 0, countRight = 0;
 
        // traverse the instruction string
        // 'move'
        for (int i = 0; i < l; i++)
        {
           // for each movement increment
            // its respective counter
            if (move[i] == 'U')
                countUp++;
 
            else if (move[i] == 'D')
                countDown++;
 
            else if (move[i] == 'L')
                countLeft++;
 
            else if (move[i] == 'R')
                countRight++;
        }
 
        // required final position of robot
        Console.WriteLine("Final Position: ("
                          + (countRight - countLeft) + ", "
                          + (countUp - countDown) + ")");
    }
 
    // Driver code
    public static void Main()
    {
        String move = "UDDLLRUUUDUURUDDUULLDRRRR";
        finalPosition(move);
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出
Final Position: (2, 3)