Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 14328   Accepted: 5393

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个点的坐标,计算这些点可以构成多少个正方形,不同顺序的相同四个点被视为同一个正方形。 思路:这里n最大是1000,显然一个点一个点的枚举不行。参考了别人的结题报告,据说有这样的定理:
   已知(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);
因此可以先枚举两个点,根据这两个点(点1 ,点2)的坐标可以得到另外两个点(点3, 点4),若在哈希表中能找得到这两个(点3, 点4),说明能构成一个正方形,
注意每个点被枚举了四次,最后要除以4;
再者就是找哈希函数,这里用的平方取余法,每输入一个点,就将这个点插入哈希表中;
这个题也受了 poj 3274的启发;
 #include<stdio.h>
#include<string.h> const int prime = ;
struct node
{
int x,y;
}pos[]; struct HashTable
{
int x;
int y;
struct HashTable* next;
}*Hash[prime];//Hash[]是指针数组,存放地址;
int n; //插入哈希表
void hash_insert(int x, int y)
{
int key = (x*x + y*y)%prime;//平方求余法;
if(!Hash[key])
{
Hash[key] = new struct HashTable;
Hash[key]->x = x;
Hash[key]->y = y;
Hash[key]->next = NULL;
}
else
{
struct HashTable *tmp = Hash[key];
while(tmp->next)
tmp = tmp->next;//开放寻址,直到next为空
//插入新结点
tmp->next = new struct HashTable;
tmp->next->x = x;
tmp->next->y = y;
tmp->next->next = NULL;
}
}
bool find(int x, int y)
{
int key = (x*x+y*y)%prime;
if(!Hash[key])
return false;//key 对应的地址不存在,
else
{
struct HashTable *tmp = Hash[key];
while(tmp)
{
if(tmp->x == x && tmp->y == y)
return true;
tmp = tmp->next;
}
return false;
}
}
int main()
{
while(scanf("%d",&n)!= EOF)
{
int i,j;
if(n == ) break;
memset(Hash,,sizeof(Hash));
for(i = ; i < n; i++)
{
scanf("%d %d",&pos[i].x,&pos[i].y);
hash_insert(pos[i].x, pos[i].y);
}
int ans = ;
for(i = ; i < n-; i++)
{
for(j = i+; j < n; j++)
{
int x1 = pos[i].x, y1 = pos[i].y;
int x2 = pos[j].x, y2 = pos[j].y;
int add_x = x1-x2,add_y = y1-y2;
int x3 = x1+add_y;
int y3 = y1-add_x;
int x4 = x2+add_y;
int y4 = y2-add_x;
if(find(x3,y3) && find(x4,y4))
ans++; x3 = x1-add_y;
y3 = y1+add_x;
x4 = x2-add_y;
y4 = y2+add_x;
if(find(x3,y3) && find(x4,y4))
ans++;
}
}
printf("%d\n",ans/);
}
return ;
}

Squares(哈希)的更多相关文章

  1. Squares<哈希>

    Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-d ...

  2. POJ-2002 Squares,哈希模板+数学公式!

                                                           Squares 题意:二维坐标轴给出n个点求有多少个正方形. 要是平时做比赛的话毫无疑问会 ...

  3. POJ 2002 Squares 哈希

    题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...

  4. poj 2002 Squares 几何二分 || 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 5749 Descript ...

  5. URAL - 1486 Equal Squares 二维哈希+二分

    During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...

  6. 【URAL 1486】Equal Squares(二维哈希+二分)

    Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...

  7. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  8. PKU 2002 Squares(二维点哈希+平方求余法+链地址法)

    题目大意:原题链接 给定平面上的N个点,求出这些点一共可以构成多少个正方形. 解题思路: 若正方形为ABCD,A坐标为(x1, y1),B坐标为(x2, y2),则很容易可以推出C和D的坐标.对于特定 ...

  9. poj Squares n个点,共能组成多少个正方形 二分 + 哈希

    题目链接:http://poj.org/problem?id=2002 测试数据: 41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20 有 ...

随机推荐

  1. Bginfo软件在域的部署和应用

    在企业的IT管理中,很多用户都不知道怎么去查看自己计算机的IP地址.登陆帐户.而对于网络管理人员来说,他们可能需要知道用户在域中登录的一些信息,如那些用户登录过,在什么时间登录,IP和MAC地址是多少 ...

  2. oracle 查看用户表数目,表大小,视图数目等

    查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * fr ...

  3. $(document).ready(function(){});不执行

    这里可能会有以下几种原因,请你挨个排查: 1.jqury的文件一定要引入1.js文件的引用路径不正确,特别是使用了命名空间,容易造成路径错误,使用绝对路径看是否成功 2.某一些函数使用错误,举个例子, ...

  4. Direct 2D实现界面库 (1)

    大学时尝试过很多次写一个UI库, 初次使用 GDI 绘图, 当时水平很低, GDI功能太弱, 以失败而告终. 之后使用 GDI+ 绘图, 当时水平依旧很低, GDI功能很强, 但效率实在太慢, 以失败 ...

  5. SGU 194. Reactor Cooling(无源汇有上下界的网络流)

    时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...

  6. 【POJ3243】【拓展BSGS】Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  7. linux负载均衡

    1.linux lvs nat实现负载均衡 添加两块网卡并开启路由管道 > /proc/sys/net/ipv4/ip_forward //开始路由管道 安装ipvsadm yum instal ...

  8. saiku

    1.saiku下载http://community.meteorite.bi/可以下载各个版本的源代码 2.下载到   saiku-latest.zip 3.解压运行比较简单     解压出来的目录: ...

  9. shell之sort

    转http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html) sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟 ...

  10. js浏览器键盘事件控制(转自新浪微博)

    js键盘事件全面控制 主要分四个部分第一部分:浏览器的按键事件第二部分:兼容浏览器第三部分:代码实现和优化第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件类型 ...