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)**

**Examples:**

Input:Sx = 0, Sy = 0, Dx = 0, Dy = 6, x = 2, y = 3

Output:Yes

(0, 0) -> (2, 3) -> (0, 6)

Input:Sx = 1, Sy = 1, Dx = 3, Dy = 6, x = 1, y = 5

Output:No

**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 **(x _{e}, y_{e})**. Define

**cnt**as

_{x}**|x**and

_{e}– Sx| / x**cnt**as

_{y}**|y**. The parity of

_{e}– Sy| / y**cnt**is the same as the parity of

_{x}**cnt**because every type of move changes the parity of both

_{y}**cnt**and

_{x}**cnt**.

_{y}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.

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if ` `// it is possible to move from source ` `// to the destination with the given moves ` `bool` `isPossible(` `int` `Sx, ` `int` `Sy, ` `int` `Dx, ` `int` `Dy, ` `int` `x, ` `int` `y) ` `{ ` ` ` `if` `(` `abs` `(Sx - Dx) % x == 0 ` ` ` `and ` `abs` `(Sy - Dy) % y == 0 ` ` ` `and (` `abs` `(Sx - Dx) / x) % 2 == (` `abs` `(Sy - Dy) / y) % 2) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `Sx = 0, Sy = 0, Dx = 0, Dy = 0; ` ` ` `int` `x = 3, y = 4; ` ` ` ` ` `if` `(isPossible(Sx, Sy, Dx, Dy, x, y)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Minimum cost path from source node to destination node via an intermediate node
- Minimum cells required to reach destination with jumps equal to cell values
- Multi Source Shortest Path in Unweighted Graph
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Minimum number of cuts required to make circle segments equal sized
- Number of decisions to reach destination
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Minimum Initial Points to Reach Destination
- Minimum number of jumps to reach end
- Minimum distance to the end of a grid from source
- Count number of ways to reach destination in a Maze
- Reach the numbers by making jumps of two given lengths
- Minimum jumps to reach last building in a matrix
- Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)