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:
- 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 a sub matrix with maximum XOR
- Find row with maximum sum in a Matrix
- Find if given matrix is Toeplitz or not
- Find N in the given matrix that follows a pattern
- Find the mean vector of a Matrix
- Find Nth term (A matrix exponentiation example)
- Find whether there is path between two cells in matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Find the sum of Eigen Values of the given N*N matrix
- Find column with maximum sum in a Matrix
- Find safe cells in 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 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.