(POJ - 2002)Squares
Time Limit: 3500MS Memory Limit: 65536K
Total Submissions: 20599 Accepted: 7919
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
题目大意:给出n个点的坐标,问能组成多少个正方形。
思路:直接暴力枚举。先任意选定两个点(x1,y1),(x2,y2),假设以这两个点为边能组成正方形,那么其余两个点可以通过全等三角形的性质计算出,画个图也很容易出来,有两种情况(上面一个,下面一个)。 只要判断理论计算出来的两点是否存在,存在
则ans++,由于这里枚举的时候正方形的每一条边长都枚举了一边所以最后ans/4才是答案。
细节见代码。
ps:尤其注意这里用memset会wa。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=20005;
bool vis[maxn<<1][maxn<<1];
struct node
{
int x,y;
}a[1005];
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
//memset(vis,0,sizeof(vis));因为数组太大,所以这里用memset会wa
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].x+=20000,a[i].y+=20000;//坐标有正有负,而数组下标没有负数
vis[a[i].x][a[i].y]=1;
}
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
{
if(i==j) continue;
int x1=a[i].x+a[i].y-a[j].y;
int y1=a[i].y+a[j].x-a[i].x;
int x2=a[j].x+a[i].y-a[j].y;
int y2=a[j].y+a[j].x-a[i].x;
if(vis[x1][y1]&&vis[x2][y2]) ans++;
x1=a[i].x+a[j].y-a[i].y;
y1=a[i].y+a[i].x-a[j].x;
x2=a[j].x+a[j].y-a[i].y;
y2=a[j].y+a[i].x-a[j].x;
if(vis[x1][y1]&&vis[x2][y2]) ans++;
}
printf("%d\n",ans/4);
for(int i=0;i<n;i++) vis[a[i].x][a[i].y]=0;
}
return 0;
}