Squares

Time Limit: 3500MS

 

Memory Limit: 65536K

Total Submissions: 11235

 

Accepted: 4096

Description

A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property. 

So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates. 

Input

The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.

Output

For each test case, print on a line the number of squares one can form from the given stars.

Sample Input

4

1 0

0 1

1 1

0 0

9

0 0

1 0

2 0

0 2

1 2

2 2

0 1

1 1

2 1

4

-2 5

3 7

0 0

5 2

0

Sample Output

1

6

1

Source

Rocky Mountain 2004

 解题报告:这道题就是给出若干个点,判断最多能组成多少个正方形;先排序,然后枚举任意两点(x1,y1)(x2,y2),则如果存在点(x1+y1-y2,y1-x1+x2)(x2+y1-y2,y2-x1+x2)则它们能构成一个正方形(这里方向是确定的,否则还有一种可能)。再利用二分查询是否存在这两个点,存在则ans ++;最后的ans要/2,因为正方形都重复算了一次。有的人用的hash,以后再利用这种方法做

代码如下:自己编了一个,看见网上都用STL写,我也稍作改变写了一个

1>自己编的二分查找2188MS

View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = 1010;
struct Point
{
int x;
int y;
}p[MAX];
bool cmp(const Point &a, const Point &b)//把x从小到大排序,若x相等则再按y从小到大排序
{
if (a.x == b.x) return a.y < b.y;
else return a.x < b.x;
}
bool Binsearch(int low, int high, Point a)//二分查找算法
{
int mid;
if (low <= high)
{
mid =low + (high - low) / 2;
if (a.x == p[mid].x && a.y == p[mid].y)//找到
{
return true;
}
if (a.x > p[mid].x ||(a.x == p[mid].x && a.y > p[mid].y))//判断的时候先判断a.x与p[mid].x;再判断y的大小
{
return Binsearch(mid + 1, high, a);//进入右半部分
}
else
{
return Binsearch(low, mid - 1, a);//进入左半部分
}
}
return false;
}
int main()
{
int n, i, ans, j;
while (scanf("%d", &n) != EOF && n)
{
for (i = 0; i < n; ++i)
{
scanf("%d%d", &p[i].x, &p[i].y);
}
sort(p, p + n, cmp);
ans = 0;
for (i =0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
Point temp;
temp.x = p[i].x + p[i].y - p[j].y;
temp.y = p[i].y - p[i].x + p[j].x;
if (!Binsearch(0, n - 1, temp))
{
continue;
}
temp.x = p[j].x + p[i].y - p[j].y;
temp.y = p[j].y - p[i].x + p[j].x;
if (!Binsearch(0, n - 1, temp))
{
continue;
}
ans ++;//两点同时满足才存在
}
}
printf("%d\n", ans / 2);
}
return 0;
}

2>用STL写的,第一次用STL,代码短而且时间也快1625MS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = 1010;
struct Point
{
int x;
int y;
}p[MAX];
bool cmp(const Point &a, const Point &b)//把x从小到大排序,若x相等则再按y从小到大排序
{
if (a.x == b.x) return a.y < b.y;
else return a.x < b.x;
}
int main()
{
int n, i, ans, j;
while (scanf("%d", &n) != EOF && n)
{
for (i = 0; i < n; ++i)
{
scanf("%d%d", &p[i].x, &p[i].y);
}
sort(p, p + n, cmp);
ans = 0;
for (i =0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
Point temp;
temp.x = p[i].x + p[i].y - p[j].y;
temp.y = p[i].y - p[i].x + p[j].x;
if(!binary_search(p, p + n, temp, cmp))
{
continue;
}
temp.x = p[j].x + p[i].y - p[j].y;
temp.y = p[j].y - p[i].x + p[j].x;
if(!binary_search(p, p + n, temp, cmp))
{
continue;
}
ans ++;
}
}
printf("%d\n", ans / 2);
}
return 0;



posted on 2012-03-27 22:22  Stephen Li  阅读(217)  评论(0编辑  收藏  举报