Regular polygon
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1123 Accepted Submission(s): 420
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
题目大意:给你n个点(x,y)其范围是-100<=x,y<=100 都为整数。然后让你求这些点能构成多少个正多边形。
解题思路:发现只有正方形的顶点才能都在整数点上面(求π的时候就是有正多边形割圆,应该是这样证明的)这样这个题目就转变成了求一共有多少个正方形。刚开始的时候想到了递归发现蠢得要死,正解应该是遍历任意的两个点然后可以根据如果存在正方形就能找出那两个点与与已知点的关系(可以画小方格把点放在小格子的交点找出其对应的关系),因为一个正方形有四条边所以一个正方形重复4次所以令最后的结果除以4就是最后的结果。
AC代码(标程代码):
#include<bits/stdc++.h>
#define mk make_pair
using namespace std;
const int N = 1e3 + 5;
map< pair<int, int>, bool> M;
int x[N], y[N], n, ans;
int main()
{
int T;
while(scanf("%d", &n)!=EOF)
{
ans=0;
M.clear();
for(int i=1;i<=n;i++)
{
scanf("%d %d",&x[i],&y[i]);
M[mk(x[i],y[i])]=1;
}
for(int i = 1; i <= n; i++)
{
for(int j = i+1; j <= n; j++)
{
int dx = y[j] - y[i];
int dy = x[i] - x[j];
int ok=0;
if(M.count(mk(x[i]+dx,y[i]+dy)))
{
ok++;
}
if(M.count(mk(x[j]+dx,y[j]+dy)))
{
ok++;
}
if(ok==2) ans++;
ok=0;
if(M.count(mk(x[i]- dx,y[i]-dy)))
{
ok++;
}
if(M.count(mk(x[j]-dx,y[j]-dy)))
{
ok++;
}
if(ok==2) ans++;
}
}
printf("%d\n",ans/4);
}
return 0;
}
此外还可以用数组来求做一个用来标记的二维数组。但是要注意数组的越界问题。