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

  1. 4
  2. 1 0
  3. 0 1
  4. 1 1
  5. 0 0
  6. 9
  7. 0 0
  8. 1 0
  9. 2 0
  10. 0 2
  11. 1 2
  12. 2 2
  13. 0 1
  14. 1 1
  15. 2 1
  16. 4
  17. -2 5
  18. 3 7
  19. 0 0
  20. 5 2
  21. 0

Sample Output

  1. 1
  2. 6
  3. 1
    题意:给出n个点的坐标,计算这些点可以构成多少个正方形,不同顺序的相同四个点被视为同一个正方形。
  4.  
  5. 思路:这里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的启发;
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. const int prime = ;
  5. struct node
  6. {
  7. int x,y;
  8. }pos[];
  9.  
  10. struct HashTable
  11. {
  12. int x;
  13. int y;
  14. struct HashTable* next;
  15. }*Hash[prime];//Hash[]是指针数组,存放地址;
  16. int n;
  17.  
  18. //插入哈希表
  19. void hash_insert(int x, int y)
  20. {
  21. int key = (x*x + y*y)%prime;//平方求余法;
  22. if(!Hash[key])
  23. {
  24. Hash[key] = new struct HashTable;
  25. Hash[key]->x = x;
  26. Hash[key]->y = y;
  27. Hash[key]->next = NULL;
  28. }
  29. else
  30. {
  31. struct HashTable *tmp = Hash[key];
  32. while(tmp->next)
  33. tmp = tmp->next;//开放寻址,直到next为空
  34. //插入新结点
  35. tmp->next = new struct HashTable;
  36. tmp->next->x = x;
  37. tmp->next->y = y;
  38. tmp->next->next = NULL;
  39. }
  40. }
  41. bool find(int x, int y)
  42. {
  43. int key = (x*x+y*y)%prime;
  44. if(!Hash[key])
  45. return false;//key 对应的地址不存在,
  46. else
  47. {
  48. struct HashTable *tmp = Hash[key];
  49. while(tmp)
  50. {
  51. if(tmp->x == x && tmp->y == y)
  52. return true;
  53. tmp = tmp->next;
  54. }
  55. return false;
  56. }
  57. }
  58. int main()
  59. {
  60. while(scanf("%d",&n)!= EOF)
  61. {
  62. int i,j;
  63. if(n == ) break;
  64. memset(Hash,,sizeof(Hash));
  65. for(i = ; i < n; i++)
  66. {
  67. scanf("%d %d",&pos[i].x,&pos[i].y);
  68. hash_insert(pos[i].x, pos[i].y);
  69. }
  70. int ans = ;
  71. for(i = ; i < n-; i++)
  72. {
  73. for(j = i+; j < n; j++)
  74. {
  75. int x1 = pos[i].x, y1 = pos[i].y;
  76. int x2 = pos[j].x, y2 = pos[j].y;
  77. int add_x = x1-x2,add_y = y1-y2;
  78. int x3 = x1+add_y;
  79. int y3 = y1-add_x;
  80. int x4 = x2+add_y;
  81. int y4 = y2-add_x;
  82. if(find(x3,y3) && find(x4,y4))
  83. ans++;
  84.  
  85. x3 = x1-add_y;
  86. y3 = y1+add_x;
  87. x4 = x2-add_y;
  88. y4 = y2+add_x;
  89. if(find(x3,y3) && find(x4,y4))
  90. ans++;
  91. }
  92. }
  93. printf("%d\n",ans/);
  94. }
  95. return ;
  96. }

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. Android内存优化之——static使用篇(使用MAT工具进行分析)

    这篇文章主要配套与Android内存优化之——static使用篇向大家介绍MAT工具的使用,我们分析的内存泄漏程序是上一篇文章中static的使用内存泄漏的比较不容易发现泄漏的第二情况和第三种情况—— ...

  2. JavaScript 数据类型转换(显式与隐式)

    一.数据类型 JS中有5中简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number.String.还有一种复杂数据类型------Object,Object本质是 ...

  3. 11.3 afternoon

    迭代40 #include<cstdio> #include<cstring> using namespace std; int n,sum,falg; ],c[]; void ...

  4. codevs 1817 灾后重建

    /* 暴力暴力 离线每次添边 堆优化dij 70 SPFA 80..... */ #include<iostream> #include<cstdio> #include< ...

  5. ASP.NET Boilerplate 邮件类使用

    在系统我们自定一个 MySettingProvider,并添加到配置集合中,定义一些邮件参数覆盖默认参数,然后通过IOC容器得到SmtpEmailSender实例,调用send方法就实现了,实现代码如 ...

  6. 【转】到底EJB是什么

    [转]到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:&quo ...

  7. ORACLE多表关联UPDATE 语句

    转载至:http://blog.itpub.net/29378313/viewspace-1064069/ 为了方便起见,建立了以下简单模型,和构造了部分测试数据:在某个业务受理子系统BSS中, SQ ...

  8. HTTP请求返回的NSData无法转换为NSString

    最近在做的一个项目中有一个功能是有一个网页,模拟http请求获取到这个网页返回的相应的数据. 在请求完成后获取到的数据为NSData类型,按照我们通常的转换为NSString的方法: NSString ...

  9. AFN的坑--NSCachedURLResponse缓存

    网络正常的情况下,如果服务器宕机或者数据库出错,会造成访问服务器报错的情况,一般报错的内容是:无法连接到服务器或者其它错误.且服务器 修复后,仍然报错.经过排查,终于找出了原因所在:AFNetwork ...

  10. 获取当前页面的url

    var url = window.location.href; var b = url.substring(url.lastIndexOf('/')+1, url.length);