Squares
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 18718 | Accepted: 7209 |
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
这个题的意思大概就是告诉你N个星星点的坐标,问你这些点最多能组成多少个正方形,4个点的暴力肯定是不行的,肯定是又得想一个暴力两个点的算法,搜了一下题解发现了一个公式:
知道两个点(x1,y1)(x2,y2)那么剩余的两个点
x3=x1+(y1-y2) y3= y1-(x1-x2)
x4=x2+(y1-y2) y4= y2-(x1-x2)
或
x3=x1-(y1-y2) y3= y1+(x1-x2)
x4=x2-(y1-y2) y4= y2+(x1-x2)
利用三角形全等可以证的,这两个点就是一条边上的两个点,然后分别向两侧(以这条边为斜边)做直角三角形,然后分别连接剩余的点,同样以连接的边做直角三角形,两个三角形是全等的,便可以得到坐标关系。
有兴趣的可以自己试一下(≧▽≦)
这样我们可以先把每个点的坐标记录下来,然后暴力两个点,看看剩余的两个点是不是存在即可。
由于这种暴力每条边都会搜索,所以我们每个正方形会记录4次,最后累加和的结果要除以4.当然直接搜索跟暴力4个点一样,我们用哈希表散列一下,
这里我直接是哈希表的下标=(x^2+y^2)%1007,试过几次,发现这个是最快的,这个空间利用率比较高是毋庸置疑的,表越大,搜索起来会更快,当然是跟数据是有关的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
const int MAXN=1007;
const int inf=0x3f3f3f;
struct node
{
int x,y;
};
int x[1005],y[1005];
vector<node>head[MAXN];
bool findx(int x,int y)
{
int d=(x*x+y*y)%MAXN,i;
int l=head[d].size();
for(i=0;i<l;++i)
{
if(head[d][i].x==x&&head[d][i].y==y)return 1;
}
return 0;
}
int main()
{
int i,j;
int n;
while(~scanf("%d",&n))
{
if(!n)break;
node t;
int ha;
for(i=0;i<MAXN;++i)head[i].clear();
for(i=0;i<n;++i)
{
scanf("%d%d",&x[i],&y[i]);
t.x=x[i];t.y=y[i];
ha=(t.x*t.x+t.y*t.y)%MAXN;
head[ha].push_back(t);
}
int ans=0;
int lx,ly,x1,y1,x2,y2;
for(i=0;i<n-1;++i)
for(j=i+1;j<n;++j)
{
lx=x[i]-x[j];
ly=y[i]-y[j];
x1=x[i]+ly,y1=y[i]-lx;
x2=x[j]+ly,y2=y[j]-lx;
if(findx(x1,y1)&&findx(x2,y2))ans++;
x1=x[i]-ly;y1=y[i]+lx;
x2=x[j]-ly;y2=y[j]+lx;
if(findx(x1,y1)&&findx(x2,y2))ans++;
}
printf("%d\n",ans>>2);
}
return 0;
}
http://www.cnblogs.com/luyingfeng/archive/2013/08/23/3277094.html
公式是从这里看的,但是发现这人完全是水过去的,暴力的姿势非常正确导致完全没问题(╯‵□′)╯︵┻━┻我也是惊呆了,表示他完全没看坐标的范围啊喂