暴力枚举四个点判断是否能组成正方形会超时,枚举 2
个点,然后暴力判断另外
2
个点,的位置是否存在。复杂度
O(N * N * logN)
。注意:斜的正方形也是正方形
如何判断另外2个点是否存在:
2
代码:
#include <bits/stdc++.h>
using namespace std;
int n,ans;
bool vis[1001][1001];
struct dot
{
int x,y;
dot& operator++()
{
x += 200;
y += 200;
return *this;
}
} a[1000001];
void che(dot b,dot c)
{
int dx = b.x - c.x,dy = b.y - c.y;
if(vis[b.x + dy][b.y - dx] && vis[c.x + dy][c.y - dx]) ans++;
if(vis[b.x - dy][b.y + dx] && vis[c.x - dy][c.y + dx]) ans++;
}
int main()
{
cin>>n;
for(int i = 1; i <= n; i++)
{
cin>>a[i].x>>a[i].y;
++a[i];
vis[a[i].x][a[i].y] = 1;
}
for(int i = 1; i <= n; i++)
for(int j = i + 1; j <= n; j++)
che(a[i],a[j]);
cout<<ans / 4;
return 0;
}