Question D: Bugs Bunny and Hunter -------------------------------- Thinking (GCD)

Topic description
Myna is hiding in the orchard beside the woods. The orchard has M × N trees, forming a matrix of M rows and N columns, and the distance between two horizontally or vertically adjacent trees is 1. Myna is under a fruit tree.
The hunter walked into the orchard with a shotgun. He climbed a fruit tree and was about to kill myna.
If there are no other fruit trees between the hunter and the starling rabbit, the hunter can see the starling rabbit.
Now that we know the location of the hunter and starling rabbit, write a program to determine whether the location of the rabbit is safe

Enter the
first line as n, indicating that there are n (n ≤ 10) sets of data. The first line of each set of data is two positive integers ax and ay, indicating the position of the hunter. The second line is two positive integers bx and by, indicating the starling Position (1 ≤ ax, ay, bx, by ≤ 100,000,000).
Output
There are n lines in the output. Each line of "yes" or "no" indicates whether the position of the starling is safe.
Sample input Copy
1
1 1
1 2
Sample output Copy
no
Resolution:
The diagonal on the matrix is ​​not blocked by trees,
so the difference between the horizontal and vertical coordinates of the two points is GCD == 1, then it must be on the diagonal Or in a straight line.

#include<bits/stdc++.h>
using namespace std;
int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,1,-1};
int n;
int sx,sy,ex,ey;
int main()
{
    scanf("%d",&n);
    while(n--)
    {
        cin>>sx>>sy>>ex>>ey;
        if(__gcd(abs(sx-ex),abs(sy-ey))!=1) cout<<"yes"<<endl;
        else cout<<"no"<<endl; 
    }
}
Published 572 original articles · praised 14 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/105244679
gcd