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:
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.
- 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 if given matrix is Toeplitz or not
- Find N in the given matrix that follows a pattern
- Find row with maximum sum in a Matrix
- Find the mean vector of a Matrix
- Find a sub matrix with maximum XOR
- Program to find transpose of a matrix
- Find maximum element of each row in a matrix
- Find orientation of a pattern in a matrix
- Find safe cells in a matrix
- Find a specific pair in Matrix
- Find unique elements in a matrix
- Find all permuted rows of a given row in a matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Program to find all types of Matrix
- Find the weight at (Xi, Yi) after M operations in a Matrix
- Find the sum of Eigen Values of the given N*N 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.