题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6055
题目大意: 在xy坐标轴上给n个点, n <= 500 (-100 <= xi, yi <= 100) 问这些点可以组成多少个不同的正多边形。
解题思路: 正多边形每个点都是整点的只有正方形, 所以暴力枚举两个点去判断是否可以形成正方形就可以了。 复杂度为O(n^2)。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int MaxN = 1000;
struct XX{
int x, y;
}a[MaxN + 5];
bool vis[MaxN + 5][MaxN + 5];
int n, ans;
bool cmp(XX a, XX b){
if(a.x == b.x) return a.y < b.y;
else return a.x < b.x;
}
int main(){
while(~scanf("%d", &n)){
memset(vis, 0, sizeof(vis));
ans = 0;
for(int i = 1; i <= n; i++){
scanf("%d %d", &a[i].x, &a[i].y);
a[i].x += 100, a[i].y += 100;
vis[a[i].x][a[i].y] = true;
}
sort(a + 1, a + 1 + n, cmp);
for(int i = 1; i < n; i++){
for(int j = i + 1; j <= n; j++){
int lx = a[i].x - a[j].x;
int ly = a[i].y - a[j].y;
if(a[i].x - ly < 0 || a[j].x - ly < 0) continue;
if(vis[a[i].x - ly][a[i].y + lx] && vis[a[j].x - ly][a[j].y + lx]) ans++;
}
}
printf("%d\n", ans / 2);
}
return 0;
}