At the Hell's Threshold ( 相似多边形的判断 )
Desmond and Thorwald rush to rescue! They are making their way through the secret underground tunnel, magically connecting the human world with Enia. But the tunnel forks into n passages, each having an ancient elven rune carefully painted above. As you might guess, not all the passages lead to Enia: some of them lead to other worlds, most of which are not suitable for life.
Ancient elven runes are the sets of luminous points, in a certain way arranged relative to each other. Each of the runes denotes the name of the world where the corresponding passage leads, in the ancient elven language. Fortunately, one tavern master had sketched the image of the desired rune on a napkin and gave it to the heroes. The image is made not in full size but is oriented correctly, i.e. from the coordinates of points on the image it is possible to get the coordinates of points on the rune by zoom (equal by all directions) and translation.
Input
The first line contains two integers separated by the space: n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 10000) — the number of passages marked by runes and the number of points each rune made of.
The next lines contains 2m integers each, separated by the spaces: xij and yij ( - 109 ≤ xij, yij ≤ + 109) — the coordinates of the j-th point on the i-th image of the rune. The first image corresponds to tavern master's drawing, and the other n correspond to the runes above passages. For each image, all points on it are different.
Output
Output n lines. In the i-th line output «YES» (without quotes) if the i-th passage leads to Enia, otherwise output «NO» (without quotes) in this line.
Examples
Input
2 3
3 4 4 6 4 5
-100 -200 100 200 100 0
-3 -4 -4 -6 -4 -5
Output
YES
NO
Input
4 4
0 0 1 2 3 6 5 10
1 1 2 3 4 7 6 11
11 21 3 5 7 13 1 1
-1 -2 0 0 4 8 2 4
0 1 1 3 2 5 3 7
Output
YES
YES
YES
NO
题意:给一个目标多边形,告诉每个顶点的坐标,下面n次询问给出的多边形是否和目标多边形相似( 可以扩大缩小,不能旋转 )。
思路:因为多边形给点的时候没有顺序,所以需要自己定义一个顺序,就是代码中的rule函数,让两个多边形的各个点顺序对应起来, 然后我们选择左上角的那个点,所有其他点和他连线,得到x差值,y差值。 如果两个多边形相似,那么这些差值都要一一对应,成倍数关系。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e4+10;
struct node {
int x,y;
}a[maxn],b[maxn];
int n,m;
bool rule( node a, node b )
{
if ( a.x==b.x ) return a.y<b.y;
return a.x<b.x;
}
int main()
{
int i,j;
cin >> n >> m;
for ( i=0; i<m; i++ ) {
scanf("%d %d",&a[i].x,&a[i].y);
}
sort(a,a+m,rule);
int g = a[1].x-a[0].x;
for ( i=1; i<m; i++ ) {
g = __gcd(g,a[i].x-a[0].x);
g = __gcd(g,a[i].y-a[0].y);
}
while ( n-- ) {
for ( i=0; i<m; i++ ) {
scanf("%d %d",&b[i].x,&b[i].y);
}
sort(b,b+m,rule);
int tg = b[1].x-b[0].x;
for ( i=1; i<m; i++ ) {
tg = __gcd(tg,b[i].x-b[0].x);
tg = __gcd(tg,b[i].y-b[0].y);
}
int isp = 1;
for ( i=1; i<m; i++ ) {
if ( (a[i].x-a[0].x)/g!=(b[i].x-b[0].x)/tg || (a[i].y-a[0].y)/g!=(b[i].y-b[0].y)/tg ) {
isp = 0;
break ;
}
}
if ( isp==1 ) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}