Check if a destination is reachable from source with two movements allowed

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

   
# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right



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.



My Personal Notes arrow_drop_up

Improved By : Sam007



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.