📜  通过旋转向量A并将向量C添加到向量中来检查是否可以到达向量B

📅  最后修改于: 2021-04-29 15:19:24             🧑  作者: Mango

给定三个二维矢量坐标ABC。该任务是对向量A执行以下操作任意次,以获得向量B

  • 将向量顺时针旋转90度。
  • 向其添加向量C。

通过上述操作可获得打印“ YES” B,否则打印“ NO”。
例子:

Input: Vector A: 2 3, Vector B: 2 3, Vector C: 0 0
Output: YES
The given vector A has coordinate (2, 3) and we need to 
convert this vector A to vector B which is also (2, 3). 
By rotating vector A 4 times by 90 degrees and adding
it to vector C(0, 0) will give us vector B(2, 3).

Input: Vector A: 0 0, Vector B: 1 1, Vector C: 2 2
Output: NO

以下是解决此问题的分步算法:

  • 将二维坐标的三个向量初始化为A(a,b),B(x,y)和C(p,q)。
  • 向量A的坐标可以是任何象限。因此,为所有象限初始化一个检查函数,并检查其中是否有一个是真的。
  • 找到ax and by,这将告诉我们要使向量B变为多少。
  • 初始化d = p * p + q * q。如果d = 0,则无需在向量A中添加任何内容。
  • 如果D> 0,则检查a * p + b * q和b * p – a * q是否在’d’的倍数中,以便可以得到向量B。

下面是上述算法的实现:

C++
// C++ program to Check if it is
// possible to reach vector B by
// Rotating vector A and adding
// vector C to it any number of times
 
#include 
using namespace std;
#define ll long long
 
// function to check if vector B is
// possible from vector A
ll check(ll a, ll b, ll p, ll q)
{
    ll d = p * p + q * q;
 
    // if d = 0, then you need to add nothing to vector A
    if (d == 0)
        return a == 0 && b == 0;
    else
        return (a * p + b * q) % d == 0 && (b * p - a * q) % d == 0;
}
 
bool check(int a, int b, int x, int y, int p, int q)
{
    // for all four quadrants
    if (check(a - x, b - y, p, q)
        || check(a + x, b + y, p, q)
        || check(a - y, b + x, p, q)
        || check(a + y, b - x, p, q))
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    // initialize all three
    // vector coordinates
 
    int a = -4, b = -2;
    int x = 0, y = 0;
    int p = -2, q = -1;
 
    if (check(a, b, x, y, p, q))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program to Check if it is
// possible to reach vector B by
// Rotating vector A and adding
// vector C to it any number of times.
   
public class GFG {
 
    // function to check if vector B is
    // possible from vector A
    static boolean check(long a, long b, long p, long q)
    {
        long d = p * p + q * q;
       
        // if d = 0, then you need to add nothing to vector A
        if (d == 0)
            return a == 0 && b == 0;
        else
            return (a * p + b * q) % d == 0 && (b * p - a * q) % d == 0;
    }
       
    static boolean check(int a, int b, int x, int y, int p, int q)
    {
        // for all four quadrants
        if (check(a - x, b - y, p, q)
            || check(a + x, b + y, p, q)
            || check(a - y, b + x, p, q)
            || check(a + y, b - x, p, q))
            return true;
        else
            return false;
    }
       
 
    // Driver code
    public static void main(String args[])
    {
        // initialize all three
        // vector coordinates
       
        int a = -4, b = -2;
        int x = 0, y = 0;
        int p = -2, q = -1;
       
        if (check(a, b, x, y, p, q))
            System.out.println("Yes");
        else
            System.out.println("No");
     
    }
    // This Code is contributed by ANKITRAI1
}


Python3
# Python3 program to Check if it
# is possible to reach vector B
# by Rotating vector A and adding
# vector C to it any number of times
 
# function to check if vector B
# is possible from vector A
def check(a, b, p, q):
 
    d = p * p + q * q;
 
    # if d = 0, then you need to
    # add nothing to vector A
    if (d == 0):
        return a == 0 and b == 0;
    else :
        return ((a * p + b * q) % d == 0 and
                (b * p - a * q) % d == 0);
 
def checks(a, b, x, y, p, q):
 
    # for all four quadrants
    if (check(a - x, b - y, p, q) or
        check(a + x, b + y, p, q) or
        check(a - y, b + x, p, q) or
        check(a + y, b - x, p, q)):
        return True;
    else:
        return False;
 
# Driver code
 
# initialize all three
# vector coordinates
a = -4;
b = -2;
x = 0;
y = 0;
p = -2;
q = -1;
 
if (checks(a, b, x, y, p, q)):
    print( "Yes");
else:
    print ("No");
 
# This code is contributed
# by Shivi_Aggarwal


C#
// C# program to Check if it is
// possible to reach vector B by
// Rotating vector A and adding
// vector C to it any number of times.
using System;
class GFG
{
 
// function to check if vector B is
// possible from vector A
static bool check(long a, long b,
                  long p, long q)
{
    long d = p * p + q * q;
     
    // if d = 0, then you need to
    // add nothing to vector A
    if (d == 0)
        return a == 0 && b == 0;
    else
        return (a * p + b * q) % d == 0 &&
               (b * p - a * q) % d == 0;
}
     
static bool check(int a, int b, int x,
                  int y, int p, int q)
{
    // for all four quadrants
    if (check(a - x, b - y, p, q) ||
        check(a + x, b + y, p, q) ||
        check(a - y, b + x, p, q) ||
        check(a + y, b - x, p, q))
        return true;
    else
        return false;
}
     
// Driver code
public static void Main()
{
    // initialize all three
    // vector coordinates
    int a = -4, b = -2;
    int x = 0, y = 0;
    int p = -2, q = -1;
     
    if (check(a, b, x, y, p, q))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed
// by ChitraNayal


PHP


Javascript


输出:
Yes