📌  相关文章
📜  检查是否允许从源头到达目的地并允许两次移动

📅  最后修改于: 2021-04-26 06:04:34             🧑  作者: Mango

给定源点(x1,y1)的坐标,确定是否有可能到达目标点(x2,y2)。从任何点(x,y)来看,只有两种类型的有效运动:
(x,x + y)和(x + y,y)。如果可能,则返回布尔值true,否则返回false。
注意:所有坐标均为正。
询问:Expedia,Telstra

例子:

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

可以使用简单的递归来解决该问题。基本情况是检查当前x或y坐标是否大于目标坐标,在这种情况下,我们返回false。如果还不是目的地,那么我们会从该点两次调用两次有效移动。
如果它们中的任何一条产生了路径,则我们返回true,否则返回false。

C++
// C++ program to check if a destination is reachable
// from source with two movements allowed
#include 
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
 $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
?>


输出:

True