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,因为只有 ...
随机推荐
- oracle 的常用语句
第一部分 基本语法 //拼接表字段 select id || 'is' || name from admin select * from emp where ename like '% ...
- JavaScript编程:使用DOM操作样式表
6.使用DOM操作样式表: 操纵元素的Style样式属性: background-color:style.backgroundColor color:style.col ...
- Android中View绘制优化二一---- 使用<include />标签复用布局文件
本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 译二: 使用<include />标签复用布局文件 翻译地址:http://de ...
- JQuery - 点击图片显示大图
效果: 目录结构: 代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="De ...
- IT忍者神龟之中的一个句sql语句——连接同一字段的全部值
Oracle能够用SYS_CONNECT_BY_PATH字符串聚合函数: SELECT LTRIM(MAX(SYS_CONNECT_BY_PATH(productname, ', ')), ', ') ...
- 14.6.1 Creating InnoDB Tables 创建InnoDB 表:
14.6.1 Creating InnoDB Tables 创建InnoDB 表: 创建一个InnoDB 表,使用CREATE TABLE 语句,你不需要指定 ENGINE=InnoDB子句 如果In ...
- LCD显示--Ht1621b芯片显示屏驱动
Ht1621b芯片显示屏驱动 关于HT1621b芯片的具体信息能够參考数据手冊上的内容:百度文库HT1621b中文资料 CS : 片选输入接一上拉电阻当/CS 为高电平读写HT1621的数据和命令无效 ...
- Swift - 使用UIScrollView实现页面滚动切换
UIScrollView提供了以页面为单位滚动显示各个子页面内容的功能,每次手指滑动后会滚动一屏的内容. 要实现该功能,需要如下操作: 1,将UIScrollView的pagingEnabled属 ...
- python基础教程_学习笔记1:序列-1
序列 数据结构:通过某种方式组织在一起的数据元素的集合,这些数据元素能够是数字或者字符,甚至能够是其它数据结构. python中,最主要的数据结构是序列. 序列中的每一个元素被分配一个序号--即元素的 ...
- SilkTest Q&A 4
Q31.如何在inc文件里面写函数? A31.在你在inc文件(例如demo.inc)里写好函数以后,你需要使用Use path/Use file来指定指定它们. 在SilkTest中->Opt ...