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

📅  最后修改于: 2021-04-29 07:24:19             🧑  作者: 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等于X2并且Y1等于Y2 ,则打印“”。
  • 如果X1不等于X2Y1不等于Y2 ,则打印“ No ”。

为了优化减法运算的复杂性,可以改为使用模数运算。只需执行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


输出:
Yes






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