题目传送门

题目大意:给你一堆点,问你能组成几个正方形。

思路:一开始想的是用对角线的长度来当哈希的key,但判断正方形会太复杂,然后就去找了一下正方形的判断方法,发现

已知: (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)

就是枚举两个点,然后算出另外两个点。在哈希表中看看能不能找到这两个点。如果只采取其中一个公式的话,切记点的坐标要排序再哈希(具体原因我也不知道,但自己举了很多样例确实是这样),你也可以把两个公式都用上,两个if得到答案。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const double PI=acos(-1.0);
int fact[10]= {1,1,2,6,24,120,720,5040,40320,362880};
const int maxn= 1010;
const int mod = 100019;
int n;
struct dian {
int x,y;
} a[maxn];
int hash[mod+10];
int next[maxn];
int gethash(int i) {
return (a[i].x*a[i].x%mod+a[i].y*a[i].y%mod)%mod;
}
bool find(int x,int y){
int hashval=(x*x%mod+y*y%mod)%mod;
for(int i=hash[hashval];i!=-1;i=next[i])
{
if(a[i].x==x&&a[i].y==y)return true;
}
return false;
}
bool cmp(dian aa,dian bb){
if(aa.x!=bb.x)
return aa.x<bb.x;
return aa.y<bb.y;
}
int main(){
while(scanf("%d",&n),n)
{
int ans=0;
memset(hash,-1,sizeof(hash));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+1,a+1+n,cmp);//非常重要 没有则wa
for(int i=1;i<=n;i++)
{
int hashval=gethash(i);//映射到哈希表里
next[i]=hash[hashval];
hash[hashval]=i;
}
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
int x=a[i].x-a[j].y+a[i].y;//算出第一个点
int y=a[i].y+a[j].x-a[i].x;
if(!find(x,y))continue;//查找
x=a[j].x-a[j].y+a[i].y;//第二个点
y=a[j].y+a[j].x-a[i].x;
if(!find(x,y))continue;//查找
ans++;
}
}
printf("%d\n",ans/2);//由于有重复计算 所以要除以二
}
}
Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 21208   Accepted: 8136

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

最新文章

  1. 阿里im即时通讯 h5 demo
  2. 区别和详解:jQuery extend()和jQuery.fn.extend()
  3. ASCII码对照表
  4. Applied Deep Learning Resources
  5. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程02:关键帧动画导入与切割》
  6. C# 我的注册表操作类
  7. day2--命令总结
  8. RobotFramework自动化测试框架的基础关键字(一)
  9. [PHP] sys_get_temp_dir()和tempnam()函数报错与环境变量的配置问题
  10. NIO SocketChannel 【链接】
  11. 从零开始学 Web 之 HTML(一)认识前端
  12. 搭建jenkins集群
  13. ESXi安装时遇到不识别的硬件的处理
  14. PyCharm 和 IntelliJ IDEA的破解激活
  15. 模仿ReentrantLock类自定义锁
  16. 出现“安全时间戳无效,因为其创建时间(“2013-10-30T14:42:07.861Z”)是将来的时间。当前时间为“2013-10-30T14:36:23.988Z”,允许的时钟偏差是“00:05:00””的原因
  17. MVC路由机制(转)
  18. python迭代器 生成器 三元运算 列表解析
  19. 软件工程-东北师大站-第四次作业PSP
  20. JUnit4 测试示例

热门文章

  1. css垂直居中方法(一)
  2. Result Maps、Auto-mapping、cache
  3. C语言获取系统时间
  4. js中的toString
  5. solr replication原理探究
  6. 保研机试训练[bailianoj]
  7. tar打包tar.gz文件
  8. keystone组件
  9. loj10088 出纳员问题
  10. vue 之 面向对象