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:
- 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 if given matrix is Toeplitz or not
- Find the mean vector of a Matrix
- Find row with maximum sum in a Matrix
- Find a sub matrix with maximum XOR
- Program to find transpose of a matrix
- Find Nth term (A matrix exponentiation example)
- Find safe cells in a matrix
- Find unique elements in a matrix
- Find number of cavities in a matrix
- Program to find all types of Matrix
- Find all permuted rows of a given row in a matrix
- Find whether there is path between two cells in 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.