Given an array of list of commands U(Up), D(Down), L(Left) and R(Right) and initial cell position (x, y) in a matrix. Find the final cell position of the object in the matrix after following the given commands. It is assumed that the final required cell position exists in the matrix.
Input : command = "DDLRULL" x = 3, y = 4 Output : (1, 5) Input : command = "LLRUUUDRRDDDULRLLUDUUR" x = 6, y = 5 Output : (6, 3)
Approach: Following are the steps:
- Count cup, cdown, cleft and cright for U(Up), D(Down), L(Left) and R(Right) movements respectively.
- Calculate final_x = x + (cright – cleft) and final_y = y + (cdown – cup).
The final cell position is (final_x, final_y)
Final Position: (1, 5)
Time Complexity: O(n), where is the number of commands.
- Distance of nearest cell having 1 in a binary matrix
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Check if a cell can be visited more than once in a String
- Transportation Problem | Set 3 (Least Cost Cell Method)
- Find if a 2-D array is completely traversed or not by following the cell values
- Find the final X and Y when they are Altering under given condition
- Final state of the string after modification
- Minimum cells traversed to reach corner where every cell represents jumps
- Find the final radiations of each Radiated Stations
- Minimum possible final health of the last monster in a game
- Minimum cells required to reach destination with jumps equal to cell values
- Find the final sequence of the array after performing given operations
- Find final value if we double after every successful search in array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.