📌  相关文章
📜  检查目标是否可以从源到达并允许两次移动 | 2套

📅  最后修改于: 2021-10-26 06:33:51             🧑  作者: Mango

给定一对坐标(X1,Y1) (源)和(X2,Y2) (目的地),任务是检查是否有可能通过从任何单元格(X,Y)的以下移动到达源的目的地) :

  1. (X + Y, Y)
  2. (X, Y + X)

注意:所有坐标都是正数,可以大到10 18

例子:

朴素方法:解决问题的最简单方法是使用递归。请参阅文章检查从源是否可到达目的地,并允许递归方法进行两次移动。

高效方法:主要思想是检查从目标坐标(X2,Y2)到源(X1,Y1)的路径是否存在。

请按照以下步骤解决问题:

  • 继续减去最大的(X2,Y2),并停止最小的(X2,Y2)如果X2小于X1Y2小于Y1。
  • 现在,比较(X1, Y1)和修改后的(X2, Y2) 。如果X1等于X2Y1等于Y2 ,则打印“”。
  • 如果X1不等于X2Y1等于,而不是Y2 ,则打印“”。

为了优化减法运算的复杂度,可以改用取运算。只需执行x2 = x2 % y2y2 = y2 % x2并检查上述必要条件。

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Check if (x2, y2) can be reached
// from (x1, y1)
bool isReachable(long long x1, long long y1,
                 long long x2, long long y2)
{
    while (x2 > x1 && y2 > y1) {
 
        // Reduce x2 by y2 until it is
        // less than or equal to x1
        if (x2 > y2)
            x2 %= y2;
 
        // Reduce y2 by x2 until it is
        // less than or equal to y1
        else
            y2 %= x2;
    }
 
    // If x2 is reduced to x1
    if (x2 == x1)
 
        // Check if y2 can be
        // reduced to y1 or not
        return (y2 - y1) >= 0
               && (y2 - y1) % x1 == 0;
 
    // If y2 is reduced to y1
    else if (y2 == y1)
 
        // Check if x2 can be
        // reduced to x1 or not
        return (x2 - x1) >= 0
               && (x2 - x1) % y1 == 0;
    else
        return 0;
}
 
// Driver Code
int main()
{
    long long source_x = 2, source_y = 10;
    long long dest_x = 26, dest_y = 12;
 
    if (isReachable(source_x, source_y,
                    dest_x, dest_y))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Python3
# Python3 program to implement
# the above approach
 
# Check if (x2, y2) can be reached
# from (x1, y1)
def isReachable(x1, y1, x2, y2):
 
    while(x2 > x1 and y2 > y1):
 
        # Reduce x2 by y2 until it is
        # less than or equal to x1
        if(x2 > y2):
            x2 %= y2
 
        # Reduce y2 by x2 until it is
        # less than or equal to y1
        else:
            y2 %= x2
 
    # If x2 is reduced to x1
    if(x2 == x1):
 
        # Check if y2 can be
        # reduced to y1 or not
        return (y2 - y1) >= 0 and (
                y2 - y1) % x1 == 0
 
    # If y2 is reduced to y1
    elif(y2 == y1):
 
        # Check if x2 can be
        # reduced to x1 or not
        return (x2 - x1) >= 0 and (
                x2 - x1) % y1 == 0
    else:
        return 0
 
# Driver Code
source_x = 2
source_y = 10
dest_x = 26
dest_y = 12
 
# Function call
if(isReachable(source_x, source_y,
                 dest_x, dest_y)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Shivam Singh


Java
// Java program to implement
// the above approach
class GFG{
 
// Check if (x2, y2) can be reached
// from (x1, y1)
static boolean isReachable(long x1, long y1,
                           long x2, long y2)
{
    while (x2 > x1 && y2 > y1)
    {
        // Reduce x2 by y2 until it is
        // less than or equal to x1
        if (x2 > y2)
            x2 %= y2;
 
        // Reduce y2 by x2 until it is
        // less than or equal to y1
        else
            y2 %= x2;
    }
 
    // If x2 is reduced to x1
    if (x2 == x1)
 
        // Check if y2 can be
        // reduced to y1 or not
        return (y2 - y1) >= 0 &&
               (y2 - y1) % x1 == 0;
 
    // If y2 is reduced to y1
    else if (y2 == y1)
 
        // Check if x2 can be
        // reduced to x1 or not
        return (x2 - x1) >= 0 &&
               (x2 - x1) % y1 == 0;
    else
        return false;
}
 
// Driver Code
public static void main(String[] args)
{
    long source_x = 2, source_y = 10;
    long dest_x = 26, dest_y = 12;
 
    if (isReachable(source_x, source_y,
                    dest_x, dest_y))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by shikhasingrajput


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Check if (x2, y2) can be reached
// from (x1, y1)
static bool isReachable(long x1, long y1,
                        long x2, long y2)
{
  while (x2 > x1 &&
         y2 > y1)
  {
    // Reduce x2 by y2
    // until it is less
    // than or equal to x1
    if (x2 > y2)
      x2 %= y2;
 
    // Reduce y2 by x2
    // until it is less
    // than or equal to y1
    else
      y2 %= x2;
  }
 
  // If x2 is reduced to x1
  if (x2 == x1)
 
    // Check if y2 can be
    // reduced to y1 or not
    return (y2 - y1) >= 0 &&
           (y2 - y1) % x1 == 0;
 
  // If y2 is reduced to y1
  else if (y2 == y1)
 
    // Check if x2 can be
    // reduced to x1 or not
    return (x2 - x1) >= 0 &&
           (x2 - x1) % y1 == 0;
  else
    return false;
}
 
// Driver Code
public static void Main(String[] args)
{
  long source_x = 2, source_y = 10;
  long dest_x = 26, dest_y = 12;
 
  if (isReachable(source_x, source_y,
                  dest_x, dest_y))
    Console.Write("Yes");
  else
    Console.Write("No");
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
Yes

时间复杂度: O(1)
辅助空间: O(1)