您的位置:首页 > 其它

POJ-2002 Squares

2018-03-29 16:51 344 查看
Squares

题目链接

Time Limit: 3500MS Memory Limit: 65536K

Total Submissions: 21115 Accepted: 8103

Descriptionv

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

题目大意

给你N个点求能组成的正方形数。

题解

首先想到一个 N^4 的方案,枚举 4 个点,再check。

实际上,对于一个正方形,我们只需要定住两个点就可以,所以定住另外两个点的位置。



对于里面的小正方形,我们假设知道左下角和右下角。那么我们就能根据横纵坐标差求出 a 和 b ,然后利用 a 和 b 求出另外两点。

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1005,maxa=20005;
int n,h[maxa<<1][maxn],num[maxa<<1];
struct js{
int x,y;
}a[maxn];
int read()
{
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
bool cmp(js a,js b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
bool fin(int x,int y)
{
if (!num[x]) return 0;
int L=1,R=num[x];
while (L<=R)
{
int mid=(R-L>>1)+L;
if (h[x][mid]==y) return 1;
else if (h[x][mid]>y) R=mid-1;else L=mid+1;
}
return 0;
}
bool check(int x,int y)
{
int A=a[y].x-a[x].x,B=a[y].y-a[x].y;
return(fin(a[x].x-B,a[x].y+A)&&fin(a[y].x-B,a[y].y+A));
}
int main()
{
while (n=read())
{
int x,lst=0,ans=0;
memset(num,0,sizeof num);
for (int i=1;i<=n;i++)
{
a[i].x=x=read()+2e4;
a[i].y=h[x][++num[x]]=read();
}
for (int i=0;i<=4e4;i++)
{
if (num[i]<=1) continue;
if (num[i]==2) {if (h[i][1]>h[i][2]) swap(h[i][1],h[i][2]);}else
sort(h[i]+1,h[i]+(1+num[i]));
}
sort(a+1,a+n+1,cmp);check(1,4);
for (int j=2;j<=n;j++)
for (int i=1;i<j;i++)
if (check(i,j)) ans++;
printf("%d\n",ans>>1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: