📌  相关文章
📜  检查是否可以从给定坐标移动到所需坐标

📅  最后修改于: 2021-04-29 12:39:08             🧑  作者: Mango

给定两个坐标(x,y)和(a,b)。查找是否可以从(a,b)到达(x,y)。
只有来自任何坐标(i,j)的可能移动

  • (ij,j)
  • (i,ij)
  • (i + j,j)
  • (i,i + j)

给定x,y,a,b可以为负。
例子:

Input : (x, y) = (1, 1) and  (a, b) = (2, 3).
Output : Yes.
(1, 1) -> (2, 1) -> (2, 3).

Input : (x, y) = (2, 1) and  (a, b) = (2, 3).
Output : Yes.

Input : (x, y) = (35, 15) and  (a, b) = (20, 25).
Output : Yes.
(35, 15) -> (20, 15) -> (5, 15) -> (5, 10) -> (5, 5) ->
(10, 5) -> (15, 5) -> (20, 5) -> (20, 25)

如果我们仔细研究这个问题,我们可以注意到,这些移动与寻找GCD的欧几里得算法相似。因此,只有当x,y的GCD等于a,b的GCD时,才可能从(x,y)到达坐标(a,b)。否则,这是不可能的。
令(x,y)的GCD为gcd。从(x,y),我们可以达到(gcd,gcd),从这一点出发,只要且仅当’a’和’b’的GCD也为gcd时,我们才能达到(a,b)。
以下是此方法的实现:

C / C++

C++
// C++ program to check if it is possible to reach
// (a, b) from (x, y).
#include 
using namespace std;
 
// Returns GCD of i and j
int gcd(int i, int j)
{
    if (i == j)
        return i;
 
    if (i > j)
        return gcd(i - j, j);
    return gcd(i, j - i);
}
 
// Returns true if it is possble to go to (a, b)
// from (x, y)
bool ispossible(int x, int y, int a, int b)
{
    // Find absolute values of all as sign doesn't
    // matter.
    x = abs(x), y = abs(y), a = abs(a), b = abs(b);
 
    // If gcd is equal then it is possible to reach.
    // Else not possible.
    return (gcd(x, y) == gcd(a, b));
}
 
// Driven Program
int main()
{
    // Converting coordinate into positive integer
    int x = 35, y = 15;
    int a = 20, b = 25;
    (ispossible(x, y, a, b)) ? (cout << "Yes") : (cout << "No");
    return 0;
}


Java
// Java program to check if it is possible
// to reach (a, b) from (x, y).
class GFG {
 
    // Returns GCD of i and j
    static int gcd(int i, int j)
    {
        if (i == j)
            return i;
 
        if (i > j)
            return gcd(i - j, j);
        return gcd(i, j - i);
    }
 
    // Returns true if it is possble to go to (a, b)
    // from (x, y)
    static boolean ispossible(int x, int y, int a, int b)
    {
 
        // Find absolute values of all as
        // sign doesn't matter.
        x = Math.abs(x);
        y = Math.abs(y);
        a = Math.abs(a);
        b = Math.abs(b);
 
        // If gcd is equal then it is possible to reach.
        // Else not possible.
        return (gcd(x, y) == gcd(a, b));
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Converting coordinate into positive integer
        int x = 35, y = 15;
        int a = 20, b = 25;
        if (ispossible(x, y, a, b))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}
// This code is contributed by Anant Agarwal.


Python
# Python program to check if it is possible to reach
# (a, b) from (x, y).
# Returns GCD of i and j
def gcd(i, j):
    if (i == j):
        return i
  
    if (i > j):
        return gcd(i - j, j)
    return gcd(i, j - i)
  
# Returns true if it is possble to go to (a, b)
# from (x, y)
def ispossible(x, y, a, b):
    # Find absolute values of all as sign doesn't
    # matter.
    x, y, a, b = abs(x), abs(y), abs(a), abs(b)
  
    # If gcd is equal then it is possible to reach.
    # Else not possible.
    return (gcd(x, y) == gcd(a, b))
  
# Driven Program
    # Converting coordinate into positive integer
x, y = 35, 15
a, b = 20, 25
if(ispossible(x, y, a, b)):
    print "Yes"
else:
    print "No"
# Contributed by Afzal Ansari


C#
// C# program to check if it is possible
// to reach (a, b) from (x, y).
using System;
 
class GFG {
 
    // Returns GCD of i and j
    static int gcd(int i, int j)
    {
        if (i == j)
            return i;
 
        if (i > j)
            return gcd(i - j, j);
        return gcd(i, j - i);
    }
 
    // Returns true if it is possble
    // to go to (a, b) from (x, y)
    static bool ispossible(int x, int y,
                           int a, int b)
    {
 
        // Find absolute values of all as
        // sign doesn't matter.
        x = Math.Abs(x);
        y = Math.Abs(y);
        a = Math.Abs(a);
        b = Math.Abs(b);
 
        // If gcd is equal then it is possible
        // to reach. Else not possible.
        return (gcd(x, y) == gcd(a, b));
    }
 
    // Driver code
    public static void Main()
    {
 
        // Converting coordinate
        // into positive integer
        int x = 35, y = 15;
        int a = 20, b = 25;
         
        if (ispossible(x, y, a, b))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
 
// This code is contributed by nitin mittal.


PHP
 $j)
        return gcd($i - $j, $j);
    return gcd($i, $j - $i);
}
 
// Returns true if it is
// possble to go to (a, b)
// from (x, y)
function ispossible($x, $y, $a, $b)
{
     
    // Find absolute values
    // of all as sign doesn't
    // matter.
    $x = abs($x);
    $y = abs($y);
    $a = abs($a);
    $b = abs($b);
 
    // If gcd is equal then
    // it is possible to reach.
    // Else not possible.
    return (gcd($x, $y) == gcd($a, $b));
}
 
// Driver Code
{
     
    // Converting coordinate
    // into positive integer
    $x = 35; $y = 15;
    $a = 20; $b = 25;
    if (ispossible($x, $y, $a, $b))
        echo( "Yes");
    else
        echo( "No");
    return 0;
}
 
// This code is contributed by nitin mittal.
?>


Javascript


输出:

Yes