Regular polygon
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2254 Accepted Submission(s): 899
Problem Description
On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make.
Input
The input file consists of several test cases. Each case the first line is a numbers N (N <= 500). The next N lines ,each line contain two number Xi and Yi(-100 <= xi,yi <= 100), means the points’ position.(the data assures no two points share the same position.)
Output
For each case, output a number means how many different regular polygon these points can make.
Sample Input
4 0 0 0 1 1 0 1 1 6 0 0 0 1 1 0 1 1 2 0 2 1
Sample Output
1 2
Source
思路:顶点在整数坐标上,只能组成正方形;枚举每个正方形的对角线,根据对角线组成的向量求出另外两个点,最后结果/2;
其中,若两个向量垂直,一个向量为(a,b),则与之垂直的两个向量分别为(b,-a),(-b,a);
代码:
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
struct point
{
int x,y;
}a[550];
map<int ,map<int,int> >mp;
int judge(point A,point B)
{
int x1=A.x,y1=A.y;
int x2=B.x,y2=B.y;
int xx1=(x1+x2+y1-y2),yy1=(x2-x1+y1+y2);
int xx2=(x1+x2+y2-y1),yy2=(x1-x2+y1+y2);
if(mp[xx1][yy1]&&mp[xx2][yy2])
return 1;
return 0;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
mp.clear();
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x+=100,y+=100;
a[i].x=x,a[i].y=y;
x*=2,y*=2;
mp[x][y]=1;
}
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(judge(a[i],a[j]))
ans++;
}
}
printf("%d\n",ans/2);
}
return 0;
}