poj 2002 Squares,hash

poj 2002 Squares

给出n个点,问能组成多少个正方形?

题解:

先把每个点hash

然后枚举两点(即枚举正方形的一条边),然后通过三角形全等,可以推出正方形的另外两点,在hash表里查找这两点看是存在,存在则 Cnt +1。

最后 answer = Cnt/4 //因为同一正方形都统计了4次。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN = 1005;
const int MOD  = 10007;

struct node {
    int x, y;
    node(int x=0, int y=0):x(x),y(y) {}
};
node P[MAXN];
int n;
int head[MOD], next[MAXN], v[MAXN], sz;

void init() {
    memset(head, -1, sizeof head );
    sz = 0;
}
void addedge(int a, int b) {
    next[sz] = head[a];
    head[a]  = sz;
    v[sz] = b;
    sz++;
}

int hash(node v) {
    return (v.x*v.x + v.y*v.y) % MOD;
}

bool find(int x, int y) {
    int h = hash(node(x,y));
    for(int j=head[h]; ~j; j = next[j]) {
        int& t = v[j];
        if(P[t].x==x && P[t].y==y)
            return true;
    }
    return false;
}
int main() {
    while(~scanf("%d", &n)&&n) {
        init();
        for(int i=0; i<n; ++i) {
            scanf("%d%d", &P[i].x, &P[i].y);
            addedge(hash(P[i]), i);
        }

        int ans = 0;
        for(int i=0; i<n; ++i)
            for(int j=i+1; j<n; ++j) {
                int xx = P[j].x - P[i].x;
                int yy = P[j].y - P[i].y;

                int x3 = P[i].x + yy;
                int y3 = P[i].y - xx;
                int x4 = P[j].x + yy;
                int y4 = P[j].y - xx;

                if(find(x3,y3) && find(x4,y4))
                    ans++;

                x3 = P[i].x - yy;
                y3 = P[i].y + xx;
                x4 = P[j].x - yy;
                y4 = P[j].y + xx;

                if(find(x3,y3) && find(x4,y4))
                    ans++;
            }
        printf("%d\n", ans/4);
    }
    return 0;
}
时间: 2024-08-14 02:34:59

poj 2002 Squares,hash的相关文章

POJ 2002 点的hash

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 abou

POJ 2002 Squares 数学 + 必须hash

http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那么,整个正方形就确定了,有两个方向. 因为, 设枚举的坐标为(x1, y1) & (x2, y2),所求的坐标是和x1,y1这个点相连,那么有方程如下. 1.垂直,向量积是0 2.边长相等,然后距离公式化简. 即可解出剩下的两个点. 然后要注意两个点要在正方形的同一侧,不然变了平行四边形了. 唤醒了

POJ 2002 Squares 计算集合 点的hash

题目大意:给出平面上的n个点,问能组成多少个正方形. 思路:一开始看时间3秒半,就想用set水过,然而失败了.没办法手写hash吧.观察坐标的范围,<20000,样例中还有负的,我们读进来的时候就将点的坐标+20000,这样避免负数,方便hash.我的哈希很弱,就是把xy坐标加起来作为哈希值,想卡的话应该很轻松.但还是过得很快. CODE: #include <cstdio> #include <cstring> #include <iostream> #incl

poj 2002 Squares

题目链接:http://poj.org/problem?id=2002 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 onl

POJ 2002 统计正方形 HASH

题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边]另外两个点的左边.例如 已知:(x1,y1) (x2,y2)则x3=x1+(y1-y2) y3= y1-(x1-x2) x4=x2+(y1-y2) y4= y2-(x1-x2)或x3=x1-(y1-y2) y3= y1+(x1-x2) x4=x2-(y1-y2) y4= y2+(x1-x2) 枚举两

POJ 2002 Squares (哈希)

题意:给定 n 个点,问有能组成多少个正方形. 析:通过直接桥梁两个顶点,然后再算另外两个,再通过哈希进行查找另外两个,这里我先是用的map,竟然卡过了3400ms多,后来改成哗哈希,900ms,哈希我也是用STL中的容器来写的,list,先枚举的那两个点是相邻的,然后再通过旋转90度,去计算另外两个. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #inclu

POJ 2002 Squares(二分)

Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 17423   Accepted: 6614 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 abou

POJ 3320 尺取法,Hash,map标记

1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识点 必须说,STL够屌.. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio>

HashMap的内部实现机制,Hash是怎样实现的,什么时候ReHash

1.HashMap的内部实现机制 HashMap是对数据结构中哈希表(Hash Table)的实现,Hash表又叫散列表.Hash表是根据关键码Key来访问其对应的值Value的数据结构,它通过一个映射函数把关键码映射到表中一个位置来访问该位置的值,从而加快查找的速度.这个映射函数叫做Hash函数,存放记录的数组叫做Hash表. 在Java中,HashMap的内部实现结合了链表和数组的优势,链接节点的数据结构是Entry<k,v>,每个Entry对象的内部又含有指向下一个Entry类型对象的引