POJ 2002 点hash
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 15489 | Accepted: 5864 |
Description
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
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
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的更多相关文章
- POJ 2002 Squares [hash]
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 16631 Accepted: 6328 Descript ...
- POJ 2002 几何+hash
题目大意: 给定1000个点,寻找有多少组四点对能组成正方形 这里的题目跟上一道做的找平行四边形类似但想法却又不相同的方法 这里找任意2个点形成的一条边,那么可以根据这两个点,找到能和他们组成正方形剩 ...
- POJ 2002 统计正方形 HASH
题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...
- POJ 2002 Squares 数学 + 必须hash
http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...
- Squares - poj 2002(hash)
枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点. #include<stdio.h> #include<string.h> #include<s ...
- poj 2002(好题 链式hash+已知正方形两点求另外两点)
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 18493 Accepted: 7124 Descript ...
- POJ 2549 二分+HASH
题目链接:http://poj.org/problem?id=2002 题意:给定一个n个数字组成的序列,然后求出4个数使得a+b+c=d,求d的最大值.其中a,b,c,d要求是给定序列的数,并且不能 ...
- POJ 2002 Squares 哈希
题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
随机推荐
- Microsoft Visual C++运行库合集下载(静默安装)
Microsoft Visual C++运行库合集下载 CN启示录2013-06-02上传 Microsoft Visual C++运行库合集由国外网友McRip制作,包含了VC2005.VC20 ...
- 图片组件——axure线框图部件库介绍
我们在后面的组件使用中,都统一使用"从部件区域拖拽图片组件到页面区域中" 1. 图片载入 1.1 将图片组件拖拽到页面区域 1.2 双击图片组件 1.3 选择合适图片,点击打开 1 ...
- JavaScript-4.6鼠标事件监听,获取鼠标坐标window.event---ShinePans
<html> <head> <meta http-equiv="content-type" content="text/html" ...
- UIGestureRecognizer在多层视图中的触发问题
在一个superview中,添加了一个subview.tap一下superview,将subview隐藏起来. 在视图superview添加一个UITapGestureRecognizer对象,在UI ...
- JSP的学习(7)——九大隐式对象之pageContext对象
本篇来讲诉JSP中九大隐式对象中最后一个,也是最重要的一个对象——pageContext对象. pageContext对象代表了该JSP页面的运行环境,它的作用有: ① 这个对象封装了对其它八大隐式对 ...
- Thinkphp入门三—框架模板、变量(47)
原文:Thinkphp入门三-框架模板.变量(47) [在控制器调用模板] display() 调用当前操作名称的模板 display(‘名字’) 调用指定名字的模板文件 控制器调用模板四种方式 ...
- CAS 单点登录
首先,何谓单点登录. 单点登录(Single Sign On),简称为 SSO,是眼下比較流行的企业业务整合的解决方式之中的一个. SSO的定义是在多个应用系统中,用户仅仅须要登录一次就能够訪问全部相 ...
- Swift编程语言学习9—— 存储属性和计算属性
属性将值跟特定的类.结构或枚举关联.存储属性存储常量或变量作为实例的一部分,计算属性计算(而不是存储)一个值.计算属性能够用于类.结构体和枚举里,存储属性仅仅能用于类和结构体. 存储属性和计算属性通经 ...
- HTML属性
- 详谈隐藏Tabbar的几种方法
如今正在写的一个项目,涉及到了使用两个TabBar,然后我须要显示当中一个的时候,然后隐藏另外一个,可是中间却出现故障了.我查了一些资料,想总结一下关于TabBar的隐藏. 第一种方法是: //隐藏t ...