Skip to content
Related Articles

Related Articles

Check if a destination is reachable from source with two movements allowed
  • Difficulty Level : Medium
  • Last Updated : 04 Jun, 2018

Given coordinates of a source point (x1, y1) determine if it is possible to reach the destination point (x2, y2). From any point (x, y) there only two types of valid movements:
(x, x + y) and (x + y, y). Return a boolean true if it is possible else return false.
Note: All coordinates are positive.
Asked in: Expedia, Telstra

Examples:

Input : (x1, y1) = (2, 10)
        (x2, y2) = (26, 12)
Output : True
(2, 10)->(2, 12)->(14, 12)->(26, 12) 
is a valid path.

Input : (x1, y1) = (20, 10)
        (x2, y2) = (6, 12)
Output : False
No such path is possible because x1 > x2
and coordinates are positive

The problem can be solved using simple recursion. Base case would be to check if current x or y coordinate is greater than that of destination, in which case we return false. If it is not the destination point yet we make two calls for both valid movements from that point.
If any of them yields a path we return true else return false.

C++




// C++ program to check if a destination is reachable
// from source with two movements allowed
#include <bits/stdc++.h>
using namespace std;
  
bool isReachable(int sx, int sy, int dx, int dy)
{
    // base case
    if (sx > dx || sy > dy)
        return false;
  
    // current point is equal to destination
    if (sx == dx && sy == dy)
        return true;
  
    // check for other 2 possibilities
    return (isReachable(sx + sy, sy, dx, dy) || 
            isReachable(sx, sy + sx, dx, dy));
}
  
// Driver code
int main()
{
    int source_x = 2, source_y = 10;
    int dest_x = 26, dest_y = 12;
    if (isReachable(source_x, source_y, dest_x, dest_y))
        cout << "True\n";
    else
        cout << "False\n";
    return 0;
}


Java




// Java program to check if a destination is 
// reachable from source with two movements 
// allowed
  
class GFG {
      
    static boolean isReachable(int sx, int sy,
                                 int dx, int dy)
    {
          
        // base case
        if (sx > dx || sy > dy)
            return false;
      
        // current point is equal to destination
        if (sx == dx && sy == dy)
            return true;
      
        // check for other 2 possibilities
        return (isReachable(sx + sy, sy, dx, dy) || 
                isReachable(sx, sy + sx, dx, dy));
    }
      
    //driver code
    public static void main(String arg[])
    {
        int source_x = 2, source_y = 10;
        int dest_x = 26, dest_y = 12;
        if (isReachable(source_x, source_y, dest_x,
                                           dest_y))
            System.out.print("True\n");
        else
            System.out.print("False\n");
    }
}
  
// This code is contributed by Anant Agarwal.


Python3




   
# Python3 program to check if
# a destination is reachable
# from source with two movements allowed
  
def isReachable(sx, sy, dx, dy):
  
    # base case
    if (sx > dx or sy > dy):
        return False
  
    # current point is equal to destination
    if (sx == dx and sy == dy):
        return True
  
    # check for other 2 possibilities
    return (isReachable(sx + sy, sy, dx, dy) or
            isReachable(sx, sy + sx, dx, dy))
  
# Driver code
source_x, source_y = 2, 10
dest_x, dest_y = 26, 12
if (isReachable(source_x, source_y, dest_x, dest_y)):
    print("True")
else:
    print("False")
      
# This code is contributed by Anant Agarwal.


C#




// C# program to check if a destination is 
// reachable from source with two movements 
// allowed
using System;
  
class GFG {
      
    static bool isReachable(int sx, int sy,
                             int dx, int dy)
    {
          
        // base case
        if (sx > dx || sy > dy)
            return false;
       
        // current point is equal to destination
        if (sx == dx && sy == dy)
            return true;
       
        // check for other 2 possibilities
        return (isReachable(sx + sy, sy, dx, dy) || 
                isReachable(sx, sy + sx, dx, dy));
    }
      
    //driver code
    public static void Main()
    {
        int source_x = 2, source_y = 10;
        int dest_x = 26, dest_y = 12;
        if (isReachable(source_x, source_y, dest_x, 
                                           dest_y))
            Console.Write("True\n");
        else
            Console.Write("False\n");
    }
}
  
// This code is contributed by Anant Agarwal.


PHP




<?php
// PHP program to check if a
// destination is reachable
// from source with two movements
// allowed
  
function isReachable($sx, $sy, $dx, $dy)
{
      
    // base case
    if ($sx > $dx || $sy > $dy)
        return false;
  
    // current point is equal 
    // to destination
    if ($sx == $dx && $sy == $dy)
        return true;
  
    // check for other 2 possibilities
    return (isReachable($sx + $sy, $sy, $dx, $dy) || 
            isReachable($sx, $sy + $sx, $dx, $dy));
}
  
    // Driver code
    $source_x = 2;
    $source_y = 10;
    $dest_x = 26;
    $dest_y = 12;
    if (isReachable($source_x, $source_y
                       $dest_x, $dest_y))
        echo "True\n";
    else
        echo "False\n";
          
// This code is contributed by Sam007
?>



Output:



True

This article is contributed by Aditi Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :