Open In App

Source to destination in 2-D path with fixed sized jumps

Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  1. (a + x, b + y)
  2. (a – x, b + y)
  3. (a + x, b – y)
  4. (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 (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.  

C++




// 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;
}


Java




// Java implementation of the approach
import java .io.*;
 
class GFG
{
     
// Function that returns true if
// it is possible to move from source
// to the destination with the given moves
static boolean isPossible(int Sx, int Sy, int Dx,
                          int Dy, int x, int y)
{
    if (Math.abs(Sx - Dx) % x == 0 &&
        Math.abs(Sy - Dy) % y == 0 &&
       (Math.abs(Sx - Dx) / x) % 2 ==
       (Math.abs(Sy - Dy) / y) % 2)
        return true;
 
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    int Sx = 0, Sy = 0, Dx = 0, Dy = 0;
    int x = 3, y = 4;
 
    if (isPossible(Sx, Sy, Dx, Dy, x, y))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by inder_verma..


Python3




# Python3 implementation of the approach
 
# Function that returns true if it is
# possible to move from source to the
# destination with the given moves
def isPossible(Sx, Sy, Dx, Dy, x, 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
Sx = 0;
Sy = 0;
Dx = 0;
Dy = 0;
x = 3;
y = 4;
 
if (isPossible(Sx, Sy, Dx, Dy, x, y)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by mits


C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// Function that returns true if
// it is possible to move from source
// to the destination with the given moves
static bool isPossible(int Sx, int Sy, int Dx,
                        int Dy, int x, int y)
{
    if (Math.Abs(Sx - Dx) % x == 0 &&
        Math.Abs(Sy - Dy) % y == 0 &&
        (Math.Abs(Sx - Dx) / x) % 2 ==
        (Math.Abs(Sy - Dy) / y) % 2)
        return true;
 
    return false;
}
 
// Driver code
static void Main()
{
    int Sx = 0, Sy = 0, Dx = 0, Dy = 0;
    int x = 3, y = 4;
 
    if (isPossible(Sx, Sy, Dx, Dy, x, y))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by chandan_jnu


PHP




<?php
// PHP implementation of the approach
 
// Function that returns true if
// it is possible to move from source
// to the destination with the given moves
function isPossible($Sx, $Sy, $Dx,
                    $Dy, $x, $y)
{
    if (abs($Sx - $Dx) % $x == 0 &&
        abs($Sy - $Dy) % $y == 0 &&
       (abs($Sx - $Dx) / $x) % 2 ==
       (abs($Sy - $Dy) / $y) % 2)
        return true;
 
    return false;
}
 
// Driver code
$Sx = 0; $Sy = 0; $Dx = 0;
$Dy = 0; $x = 3; $y = 4;
 
if (isPossible($Sx, $Sy, $Dx,
               $Dy, $x, $y))
    echo("Yes");
else
    echo("No");
 
// This code is contributed
// by Code_Mech
?>


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function that returns true if
// it is possible to move from source
// to the destination with the given moves
function isPossible(Sx, Sy, Dx, Dy, x, y)
{
    if (Math.abs(Sx - Dx) % x == 0 &&
        Math.abs(Sy - Dy) % y == 0 &&
        (Math.abs(Sx - Dx) / x) % 2 ==
        (Math.abs(Sy - Dy) / y) % 2)
        return true;
 
    return false;
}
 
// Driver code
let Sx = 0, Sy = 0, Dx = 0, Dy = 0;
let x = 3, y = 4;
 
if (isPossible(Sx, Sy, Dx, Dy, x, y))
    document.write("Yes");
else
    document.write("No");
     
// This code is contributed by mukesh07
 
</script>


Output: 

Yes

 

Time Complexity: O(1)

Auxiliary Space: O(1)



Last Updated : 23 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads