Given the source point and the destination point of the path and two integers x and y. The task is to check whether it is possible to move from source to the destination with the below moves, If current position is (a, b) then the valid moves are:
- (a + x, b + y)
- (a – x, b + y)
- (a + x, b – y)
- (a – x, b – y)
Input: Sx = 0, Sy = 0, Dx = 0, Dy = 6, x = 2, y = 3
(0, 0) -> (2, 3) -> (0, 6)
Input: Sx = 1, Sy = 1, Dx = 3, Dy = 6, x = 1, y = 5
Approach: Let’s approach this problem as if the steps were (a, b) -> (a + x, 0) or (a, b) -> (a – x, 0) or (a, b) -> (0, b + y) or (a, b) -> (0, b – y). Then the answer is Yes if |Sx – Dx| mod x = 0 and |Sy – Dy| mod y = 0.
It’s easy to see that if the answer to this problem is NO then the answer to the original problem is also NO.
Let’s return to the original problem and take a look at some sequence of steps. It ends in some point (xe, ye). Define cntx as |xe – Sx| / x and cnty as |ye – Sy| / y . The parity of cntx is the same as the parity of cnty because every type of move changes the parity of both cntx and cnty.
So the answer is Yes if |Sx – Dx| mod x = 0, |Sy – Dy| mod y = 0 and |Sx – Dx| / x mod 2 = |Sy – Dy| / y mod 2.
Below is the implementation of the above approach.
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Minimum cost path from source node to destination node via an intermediate node
- Shortest Path with even number of Edges from Source to Destination
- Queries to check if a path made up of even numbers from source to destination exists in a Matrix
- Minimum cells required to reach destination with jumps equal to cell values
- Print all shortest paths between given source and destination in an undirected graph
- Minimize steps defined by a string required to reach the destination from a given source
- Check if a destination is reachable from source with two movements allowed | Set 2
- Path traversed using exactly M coins in K jumps
- Multi Source Shortest Path in Unweighted Graph
- Minimum number of cuts required to make circle segments equal sized
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Percentage increase in the volume of cuboid if length, breadth and height are increased by fixed percentages
- Check if digit cube limit of an integer arrives at fixed point or a limit cycle
- Minimum Initial Points to Reach Destination
- Count number of ways to reach destination in a Maze
- Minimum number of moves required to reach the destination by the king in a chess board
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.