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. Objective-C中的@Property详解

    Objective-C中的@Property详解 @Property (属性) class vairs 这个属性有nonatomic, strong, weak, retain, copy等等 我把它 ...

  2. JS cookie 读写操作

    /*** ** 功能: cookie操作对象 ***/ var cookies = { /*** ** 功能: 写入cookie操作 ** 参数: name cookie名称 ** value coo ...

  3. 手势交互之GestureOverlayView

    一种用于手势输入的透明覆盖层,可以覆盖在其他空间的上方,也可包含在其他控件 android.gesture.GestureOverlayView 获得手势文件 需要用GesturesBuilder,如 ...

  4. Android 通信机制Message、Handler 的用法

    Android中提供了通信机制,Message.Handler 等,Message用于在子线程中传递数据,Handler用于发送数据到主线程中, 下面介绍基于Message.Handler的计时器 i ...

  5. Array,ArrayList 和 List<T>的选择和性能比较.

    Array Class Provides methods for creating, manipulating, searching, and sorting arrays, thereby serv ...

  6. Linux软件

    网上下载:Chrome Browser for Linux; sqlite;  WPS; symbol-fonts; 软件中心:Terminator; Code::Blocks IDE;  新立得软件 ...

  7. tomcat的webapp下的root文件夹的作用是什么

    1.基本一样..只是表示不同的tomcat的http路径而已. root目录默认放的是tomcat自己的一个项目,如:http://localhost:8080/默认访问root项目 对于webapp ...

  8. 【原】ios tableViewCell 自适应高度

    原文:http://www.cnblogs.com/A--G/p/4819051.html 前言:之前在做一个类似微博的小需求时候,用table view实现了微博文字和图片等等的基本展示,由于文字和 ...

  9. JavaScript学习笔记之原型对象

    本文是学习<JavaScript高级程序设计>第六章的笔记. JS中,便于批量创建对象的三种模式: 1.工厂模式:用一个函数封装创建对象的细节,传入必要的参数,在函数内部new一个对象并返 ...

  10. 【深度解析】Google第二代深度学习引擎TensorFlow开源

    作者:王嘉俊 王婉婷 TensorFlow 是 Google 第二代深度学习系统,今天宣布完全开源.TensorFlow 是一种编写机器学习算法的界面,也可以编译执行机器学习算法的代码.使用 Tens ...