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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Minimum number of flips required such that the last cell of matrix can be reached from any other cell
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
- Final Matrix after incrementing submatrices by K in range given by Q queries
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Distance of nearest cell having 1 in a binary matrix
- Find minimum moves to bring all elements in one cell of a matrix
- Sum of cost of all paths to reach a given cell in a Matrix
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Maximize path sum from top-left cell to all other cells of a given Matrix
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Maximum XOR of a path from top-left to bottom-right cell of given Matrix
- Find the final X and Y when they are Altering under given condition
- Find the final radiations of each Radiated Stations
- Minimum possible final health of the last monster in a game
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.