您的位置:首页 > 其它

Squares

2015-08-04 11:46 295 查看
Squares

Time Limit: 3500MS Memory Limit: 65536K

Total Submissions: 17462 Accepted: 6634

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

在推正方形顶点时,多亏了金巨巨,金巨巨就是给力

#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout)

const int MAX = 1010;

struct node
{
int x;
int y ;
}Point[MAX];

bool cmp(node b,node c)
{
if(b.x<c.x||(b.x==c.x&&b.y<c.y))
{
return true;
}
return false;
}

bool Look(int low,int high,int x,int y)//二分查找
{
int i=low,j=high;
while(i<=j)
{
int mid=(i+j)/2;
if(Point[mid].x==x&&Point[mid].y==y)
{
return true;
}
if(Point[mid].x<x)
{
i=mid+1;
}
else if(Point[mid].x>x)
{
j=mid-1;
}
else if(Point[mid].x==x)
{
if(Point[mid].y<y)
{
i=mid+1;
}
else if(Point[mid].y>y)
{
j=mid-1;
}
}
}
return false;
}
int main()
{

int n;
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++)
{
scanf("%d %d",&Point[i].x,&Point[i].y);
}
sort(Point,Point+n,cmp);
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int ans=(Point[i].x+Point[j].x-Point[j].y+Point[i].y);//计算正方形的其余的两个顶点
int ant=(Point[i].y+Point[j].y+Point[j].x-Point[i].x);
bool flag=false;
if(ans%2==0&&ant%2==0)
{
flag=Look(0,n-1,ans/2,ant/2);
}

if(!flag)
{
continue;
}
flag=false;
ans=(Point[i].x+Point[j].x+Point[j].y-Point[i].y);
ant=(Point[i].y+Point[j].y-Point[j].x+Point[i].x);
if(ans%2==0&&ant%2==0)
{
flag=Look(0,n-1,ans/2,ant/2);
}
if(flag)
{
sum++;
}
}
}
printf("%d\n",sum/2);//对于每个正方形都会查找到两次,所以除二
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: