Given four integers x1, y1 and x2, y2 which represent two locations in an infinite 2D-Matrix, the task is to find whether it is possible to move from (x1, y1) to (x2, y2) in a single move, either left, right, up or down. Note that the move will be repeated until the destination is reached. If it is impossible to reach (x2, y2) output -1.
Input: x1 = 0, y1 = 0, x2 = 1, y2 = 0
Destination is just below the starting point.
Input: x1 = 0, y1 = 0, x2 = 1, y2 = 1
It is impossible to reach (1, 1) from (0, 0) in a single move.
Approach: Check if the coordinates are either in the same row or in the same column then only its possible to reach the final destination. Then print the move according to the direction of the destination.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if the rows of a binary matrix can be made unique by removing a single column
- Rat in a Maze Problem when movement in all possible directions is allowed
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Find sub-matrix with the given sum
- Find row with maximum sum in a Matrix
- Find N in the given matrix that follows a pattern
- Find the mean vector of a Matrix
- Find if given matrix is Toeplitz or not
- Find a sub matrix with maximum XOR
- Find the sum of Eigen Values of the given N*N matrix
- Find orientation of a pattern in a matrix
- Find a specific pair in Matrix
- Find column with maximum sum in a Matrix
- Program to find transpose of a matrix
- Find all permuted rows of a given row in a matrix
- Find whether there is path between two cells in matrix
- Find unique elements in a matrix
- Find number of cavities in a matrix
- Find the weight at (Xi, Yi) after M operations in a Matrix
- Program to find the Sum of each Row and each Column of a Matrix
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.