#include <stdio.h>
#include <string.h>
#define N 1000
struct Point {
int x;
int y;
};
struct Point point[N];
int n; /* 点的个数 */
/* 由于点已经按照坐标排序过,所以采用二分查找
* 搜索点(x,y)是否存在,存在返回1,否则返回0
*/
int bsearch(int x, int y)
{
int m, s, t;
s = 0;
t = n-1;
while (s <= t) {
m = (s+t)/2;
if (point[m].x == x && point[m].y == y) return 1;
if (point[m].x > x || (point[m].x == x && point[m].y > y)) {
t = m-1;
}
else {
s = m+1;
}
}
return 0;
}
int main()
{
int x, y, i, j, count;
while (scanf("%d", &n), n) {
count = 0;
for (i = 0; i < n; i++) {
scanf("%d %d", &x, &y);
//插入法对点排序,按照x从小到大,y从小到大,且x优先排列的方式
for (j = i-1; j >= 0; j--) {
if (point[j].x > x || (point[j].x == x && point[j].y > y)) {
point[j+1] = point[j];
} else {
break;
}
}
point[j+1].x = x;
point[j+1].y = y;
}
/* 枚举所有边,对每条边的两个顶点可以
* 确定一个唯一的正方形,并求出另外两个顶点的坐标
*/
for (i = 0; i < n; i++) {
for (j = (i+1); j < n; j++) {
//计算第三个点的坐标,搜索其是否存在
x = point[i].y-point[j].y+point[i].x;
y = point[j].x-point[i].x+point[i].y;
if (bsearch(x,y) == 0) {
continue;
}
//计算第四个点的坐标,搜索其是否存在
x = point[i].y-point[j].y+point[j].x;
y = point[j].x-point[i].x+point[j].y;
if (bsearch(x, y)) {
count++;
}
}
}
printf("%d\n", count/2);
}
return 0;
}
poj 2002
最新推荐文章于 2018-02-04 21:40:21 发布