POJ 2002 Squares 计算集合 点的hash
来源:互联网 发布:光纤网络是哪个公司的 编辑:程序博客网 时间:2024/05/16 19:42
题目大意:给出平面上的n个点,问能组成多少个正方形。
思路:一开始看时间3秒半,就想用set水过,然而失败了。没办法手写hash吧。观察坐标的范围,<20000,样例中还有负的,我们读进来的时候就将点的坐标+20000,这样避免负数,方便hash。我的哈希很弱,就是把xy坐标加起来作为哈希值,想卡的话应该很轻松。但还是过得很快。
CODE:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 1010using namespace std;struct Point{int x,y;Point(int _ = 0,int __ = 0):x(_),y(__) {}bool operator ==(const Point &a)const {return (x == a.x && y == a.y);}Point operator -(const Point &a)const {return Point(x - a.x,y - a.y);}Point operator +(const Point &a)const {return Point(x + a.x,y + a.y);}void Read() {scanf("%d%d",&x,&y);x += 20000,y += 20000;}}point[MAX];struct HashSet{int head[80010],total;int next[MAX];Point val[MAX];void Insert(const Point &p) {int x = p.x + p.y;next[++total] = head[x];val[total] = p;head[x] = total;}bool Find(const Point &p) {int x = p.x + p.y;for(int i = head[x]; i; i = next[i])if(val[i] == p)return true;return false;}void Clear() {memset(head,0,sizeof(head));total = 0;}}hash;int points;int main(){while(scanf("%d",&points),points) {for(int i = 1; i <= points; ++i) {point[i].Read();hash.Insert(point[i]);}int ans = 0;for(int i = 1; i <= points; ++i)for(int j = i + 1; j <= points; ++j) {Point v = point[j] - point[i];Point _v(v.y,-v.x);if(hash.Find(point[i] + _v) && hash.Find(point[j] + _v))++ans;Point __v(-v.y,v.x);if(hash.Find(point[i] + __v) && hash.Find(point[j] + __v))++ans;}printf("%d\n",ans / 4);hash.Clear();}return 0;}
0 0
- POJ 2002 Squares 计算集合 点的hash
- poj 2002 Squares(枚举+点hash)
- POJ 2002 Squares hash/计算几何
- POJ 2002 Squares Hash
- POJ 2002 Squares -- Hash
- poj 2002 Squares,hash
- POJ 2002 Squares (Hash)
- poj 2002 Squares Hash
- POJ 2002 Squares hash
- poj 2002 Squares(hash)
- poj 2002 Squares (hash 拉链 点集)
- POJ 2002 Squares(计算几何 找正方形 hash枚举)
- POJ 2002 - Squares(hash)
- POJ 2002 Squares(hash)
- POJ-2002-Squares(hash)
- poj 2002 Squares【HASH】POJ最快
- POJ 2002 点的hash
- Poj 2002 Squares (正方形个数 Hash)
- C++使用MD5
- 数字分隔(二)
- 2009年9月全国计算机等级考试四级笔试试卷网络工程师
- BZOJ 2730 HNOI2012 矿场搭建 Tarjan
- 挣值分析:预测技术
- POJ 2002 Squares 计算集合 点的hash
- Fragments之间的交互
- Android 调试 中 conversion to dalvik format failed with error 1的解决办法汇总
- iOS 多线程的理解
- 日语学习之新版初级标准日本语-20 スミスさんはピアノを弾くことができます
- 浅谈C++多态性
- void pointer
- find the longest of the shortest (hdu 1595 SPFA+枚举)
- 《Linux设备驱动程序》——调试技术