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
- 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
- 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
- Minimum cells required to reach destination with jumps equal to cell values
- Find final value if we double after every successful search in array
- Program to check diagonal matrix and scalar matrix
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N matrix
- Check if it is possible to make the given matrix increasing matrix or not
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.