Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 15489   Accepted: 5864

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

二维平面给定一堆点,求能够组成正方形的个数,点数为1000。因此不能枚举四个点推断。

比較优化的方法是将全部点hash。然后枚举两个点,计算出另外两个点的坐标,然后在hash表里查找,最后结果除以4,由于每一条边被统计了4次。

代码:

/* ***********************************************
Author :_rabbit
Created Time :2014/5/11 8:26:00
File Name :20.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100009;
class HASH{
public:
struct Node{
int next,to;
Node(int _next=0,int _to=0){
next=_next;to=_to;
}
}edge[10010];
int tol,head[maxn+10];
void clear(){
memset(head,-1,sizeof(head));tol=0;
}
void add(int x,int y){
if(find(x,y))return;
int t=(x+maxn)%maxn;
edge[tol]=Node(head[t],y);
head[t]=tol++;
}
int find(int x,int y){
int t=(x+maxn)%maxn;
for(int i=head[t];i!=-1;i=edge[i].next)
if(edge[i].to==y)
return 1;
return 0;
}
}mi;
int x[1010],y[1010];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int n;
while(~scanf("%d",&n)&&n){
mi.clear();
for(int i=0;i<n;i++){
scanf("%d%d",&x[i],&y[i]);
mi.add(x[i],y[i]);
// cout<<"hhh "<<endl;
}
ll ans=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++){
// cout<<"ddd"<<endl;
ll x3,y3,x4,y4;
x3=x[i]+(y[j]-y[i]);y3=y[i]-(x[j]-x[i]);
x4=x[j]+(y[j]-y[i]);y4=y[j]-(x[j]-x[i]);
if(mi.find(x3,y3)&&mi.find(x4,y4))ans++;
x3=x[i]-(y[j]-y[i]);y3=y[i]+(x[j]-x[i]);
x4=x[j]-(y[j]-y[i]);y4=y[j]+(x[j]-x[i]);
if(mi.find(x3,y3)&&mi.find(x4,y4))ans++;
// cout<<"ddd"<<endl;
}
ans/=4;
cout<<ans<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ 2002 点hash的更多相关文章

  1. POJ 2002 Squares [hash]

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 16631   Accepted: 6328 Descript ...

  2. POJ 2002 几何+hash

    题目大意: 给定1000个点,寻找有多少组四点对能组成正方形 这里的题目跟上一道做的找平行四边形类似但想法却又不相同的方法 这里找任意2个点形成的一条边,那么可以根据这两个点,找到能和他们组成正方形剩 ...

  3. POJ 2002 统计正方形 HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...

  4. POJ 2002 Squares 数学 + 必须hash

    http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...

  5. Squares - poj 2002(hash)

    枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点. #include<stdio.h> #include<string.h> #include<s ...

  6. poj 2002(好题 链式hash+已知正方形两点求另外两点)

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 18493   Accepted: 7124 Descript ...

  7. POJ 2549 二分+HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定一个n个数字组成的序列,然后求出4个数使得a+b+c=d,求d的最大值.其中a,b,c,d要求是给定序列的数,并且不能 ...

  8. POJ 2002 Squares 哈希

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

  9. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

随机推荐

  1. unix domain IPC 进程间通信简析

    Linux系统有多种进程间通信方式,如信号.消息队列.管道等,socket是其中一种,socket使用unix domain 模式进行进程间通信 //服务端代码 #include <stdio. ...

  2. ACM POJ 2192 Zipper

    题目大意:输入字符串a,b,c 要求推断c是否有a,b中的个字符保持原有顺序组合而成. 算法思想: DP 用dp[i][j]表示a的前0~i-1共i个字符和b的前0~j-1共j个字符是否构成c[i+j ...

  3. 论文阅读笔记 - Mesos: A Platform for Fine-Grained ResourceSharing in the Data Center

    作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ 更多论文阅读笔记 http:/ ...

  4. 进阶:案例三: Upload File using WebDynpro

    1.节点创建,其中DATASOURCE存放uploadfile名称 2.layout布局 3.upload事件代码: method ONACTIONUPLOAD . DATA: lo_Node typ ...

  5. Servlet的学习之Request请求对象(3)

    本篇接上一篇,将Servlet中的HttpServletRequest对象获取RequestDispatcher对象后能进行的[转发]forward功能和[包含]include功能介绍完. 首先来看R ...

  6. PHP开发-上传文件

    <?php /****************************************************************************** 参数说明: $max_ ...

  7. 中间件(Middleware)

    中间件(Middleware) ASP.NET Core开发,开发并使用中间件(Middleware). 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 每个组件选择是否传递给管道中的下 ...

  8. 14.9.2 Specifying the Row Format for a Table 指定 表的行格式

    14.9.2 Specifying the Row Format for a Table 指定 表的行格式 mysql> SHOW TABLE STATUS\G; *************** ...

  9. Linux系统基础命令

    这是看itercast的学习笔记 Linux系统基础命令 日期时间 命令date用以查看.设置当前系统时间:格式化显示时间: +%Y--%m--%d 命令hwclock(clock)用以显示硬件时钟时 ...

  10. WPF入门介绍

    Windows Vista已经于2007年1月30正式发行零售版本,安装Vista的计算机将会大量出现.在Vista时代,身为编程员,就一定要具备Vista桌面应用开发的能力.而开发Vista桌面应用 ...