POJ 2002 Squares 计算集合 点的hash

题目大意:给出平面上的n个点,问能组成多少个正方形。


思路:一开始看时间3秒半,就想用set水过,然而失败了。没办法手写hash吧。观察坐标的范围,<20000,样例中还有负的,我们读进来的时候就将点的坐标+20000,这样避免负数,方便hash。我的哈希很弱,就是把xy坐标加起来作为哈希值,想卡的话应该很轻松。但还是过得很快。


CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1010
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值