Description
有N个格点,取若干个点组成正多边形,问有几个
Algorithm
格点正方形只有正方形
枚举4个点
然后就是这题算法
Code
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 20 + 9;
struct P
{
int x, y;
};
int dist2(P a, P b)
{
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
bool ok(P a, P b, P c, P dd)
{
int d[6];
d[0] = dist2(a, b);
d[1] = dist2(a, c);
d[2] = dist2(a, dd);
d[3] = dist2(b, c);
d[4] = dist2(b, dd);
d[5] = dist2(c, dd);
sort(d, d + 6);
if (d[0] == d[1] && d[1] == d[2] && d[2] == d[3] && d[4] == d[5] && d[3] * 2 == d[4]) return true;
return false;
}
int main()
{
int n;
while (cin >> n)
{
P a[maxn];
for (int i = 0; i < n; i++)
cin >> a[i].x >> a[i].y;
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = 0;j < i; j++)
for (int k = 0; k < j;k++)
for (int kk = 0; kk < k; kk++)
if (ok(a[i], a[j], a[k], a[kk]))
ans++;
cout << ans << endl;
}
}