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

22 Oct, 2021
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, TelstraExamples:  
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++
Java
Python3
C#
PHP
Javascript
// 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 codeint 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 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 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 codesource_x, source_y = 2, 10dest_x, dest_y = 26, 12if (isReachable(source_x, source_y, dest_x, dest_y)):    print("True")else:    print("False")     # This code is contributed by Anant Agarwal.
// C# program to check if a destination is// reachable from source with two movements// allowedusing 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 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?>
<script> // Javascript 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 program         let source_x = 2, source_y = 10;        let dest_x = 26, dest_y = 12;        if (isReachable(source_x, source_y, dest_x,                                           dest_y))            document.write("True\n");        else            document.write("False\n");         </script>
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 write.geeksforgeeks.org or mail your article to review-team@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. 
Sam007
sanjoy_62
khushboogoyal499
Expedia
Telstra
Recursion
Expedia
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Backtracking | Introduction
Recursive Functions
Print all subsequences of a string
Check if a number is Palindrome
Count all possible paths from top left to bottom right of a mXn matrix
Combinational Sum
Recursive Bubble Sort
Reverse a stack using recursion
3 Different ways to print Fibonacci series in Java
Flood fill Algorithm - how to implement fill() in paint?