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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Sum of cost of all paths to reach a given cell in a Matrix
- Distance of nearest cell having 1 in a binary matrix
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- 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?
- Find the final X and Y when they are Altering under given condition
- Final state of the string after modification
- Check if a cell can be visited more than once in a String
- Find the final radiations of each Radiated Stations
- Minimum possible final health of the last monster in a game
- Transportation Problem | Set 3 (Least Cost Cell Method)
- Find the final sequence of the array after performing given operations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.