Position of robot after given movements

Given a robot which can only move in four directions, UP(U), DOWN(D), LEFT(L), RIGHT(R). Given a string consisting of instructions to move. Output the coordinates of a robot after executing the instructions. Initial position of robot is at origin(0, 0).

Examples:

```Input : move = "UDDLRL"
Output : (-1, -1)
Move U : (0, 0)--(0, 1)
Move D : (0, 1)--(0, 0)
Move D : (0, 0)--(0, -1)
Move L : (0, -1)--(-1, -1)
Move R : (-1, -1)--(0, -1)
Move L : (0, -1)--(-1, -1)

Therefore final position after the complete
movement is: (-1, -1)

Input : move = "UDDLLRUUUDUURUDDUULLDRRRR"
Output : (2, 3)
```

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Count number of up movements (U), down movements (D), left movements (L) and right movements (R) as countUp, countDown, countLeft and countRight respectively. Final x-coordinate will be
(countRight – countLeft) and y-coordinate will be (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 program to test above ` `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 program to test above ` `    ``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 program to test above ` `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 program to test above ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``String move = ``"UDDLLRUUUDUURUDDUULLDRRRR"``; ` `        ``finalPosition(move); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

PHP

 ` `

Output:

```Final Position: (2, 3)
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Sam007, nidhi_biet, 29AjayKumar

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.