题意:给出平面上n个点的坐标,求这些点所能组成的正方形的个数。
思路:对n个点排序后,枚举对角线,计算出另外对角的坐标,二分查找它们是否在这n个点中,如果都在,则计数器增1。
利用三角函数公式容易推出另外两点坐标。注意一个合法的正方形会被计数2次,故最终答案要除2。
-------------------------------------------------------------------------------------------------------------------------------------------------------------
以上为转载,自己懒得说了。
下面是代码:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
struct T { int x,y; };
int n;
T cor[1002];
bool find( double x, double y )
{
int l = 0, r = n-1, mid;
T t;
while ( l <= r ) //注意一定要<=,<是不对的,找了很久......
{
mid = (l+r)/2;
t = cor[mid];
if ( t.x > x ) r = mid-1;
else if ( t.x < x ) l = mid+1;
else
{
if ( t.y > y ) r = mid-1;
else if ( t.y < y ) l = mid+1;
else return true;
}
}
return false;
}
bool cmp( T a, T b )
{
if ( a.x == b.x ) return a.y < b.y;
else return a.x < b.x;
}
int main()
{
int i, j, k, ans;
double x0, y0, x1, y1;
freopen( "ex.in", "r", stdin );
while ( scanf("%d", &n) )
{
if ( n == 0 ) break;
for ( i = 0; i < n; i++ )
scanf( "%d%d", &cor[i].x, &cor[i].y );
sort(cor,cor+n,cmp);
ans = 0;
for ( i = 0; i < n; i++ )
for ( j = i+1; j < n; j++ )
{
x0 = (cor[i].x+cor[j].x-cor[j].y+cor[i].y)*0.5;
y0 = (cor[i].y+cor[j].y-cor[i].x+cor[j].x)*0.5;
x1 = (cor[i].x+cor[j].x-cor[i].y+cor[j].y)*0.5;
y1 = (cor[i].y+cor[j].y-cor[j].x+cor[i].x)*0.5;
if ( floor(x0)!=x0 || floor(x1)!=x1 || floor(y0)!=y0 || floor(y1)!=y1 ) continue;
if ( find(x0,y0) && find(x1,y1) ) ans++;
}
printf( "%d\n", ans/2);
}
}