这道题的意思是:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少。相同的四个点,不同顺序构成的正方形视为同一正方形。
一开始想到的是暴搜所有的点,但是做之前习惯的看了一下题解、、发现暴搜过不了啊、、还好找到了由两个定点求其他两个顶点的数学方法,一开始感觉很高端啊、、后来证明了一下发现就是全等三角形的变形得来的啊、、初中数学啊、、伤不起啊、、呵呵呵、、我是跟牛牛学的把大于等于0,与小于0分开来存,这样可以更好地减少hash冲突啊、、、
Squares
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 14400 | Accepted: 5419 |
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.
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct p
{
int x, y;
} next[1005];
struct node
{
struct p po[1005];
} hash1[40005], hash2[40005];
int head1[40005], head2[40005];
int search(int x, int y)
{
int sum = x+y;
if(sum >= 0)
{
for(int i = 0; i < head1[sum]; i++)
if(hash1[sum].po[i].x == x && hash1[sum].po[i].y == y)
return 1;
}
else
{
sum *= -1;
for(int i = 0; i < head2[sum]; i++)
if(hash2[sum].po[i].x == x && hash2[sum].po[i].y == y)
return 1;
}
return 0;
}
int main()
{
int n;
while(~scanf("%d",&n) && n)
{
int i, sum;
memset(head1 , 0 , sizeof(head1));
memset(head2 , 0 , sizeof(head2));
for(i = 0; i < n; i++)
{
scanf("%d %d",&next[i].x, &next[i].y);
sum = next[i].x+next[i].y;
if(sum >= 0)
{
hash1[sum].po[head1[sum]].x = next[i].x;
hash1[sum].po[head1[sum]].y = next[i].y;
head1[sum]++;
}
else
{
sum *= -1;
hash2[sum].po[head2[sum]].x = next[i].x;
hash2[sum].po[head2[sum]].y = next[i].y;
head2[sum]++;
}
}
int x1, y1, x2, y2, cnt = 0, j;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(i == j)
continue;
x1 = next[i].x + next[i].y - next[j].y;
y1 = next[i].y - next[i].x + next[j].x;
x2 = next[j].x + next[i].y - next[j].y;
y2 = next[j].y - next[i].x + next[j].x;
if(search(x1,y1) && search(x2, y2))
cnt ++;
x1 = next[i].x - next[i].y + next[j].y;
y1 = next[i].y + next[i].x - next[j].x;
x2 = next[j].x - next[i].y + next[j].y;
y2 = next[j].y + next[i].x - next[j].x;
if(search(x1,y1) && search(x2, y2))
cnt ++;
}
}
printf("%d\n",cnt/4);
}
return 0;
}