POJ 2002 hash+枚举

题意:给出n个点,问组成了多少个正方形。

枚举两个点,求出另两个点然后hash看是否存在。不会hash,基本上是搬了黄大神的代码= =吗。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mm=1005;
const int mod=100007;
struct point
{
    int x,y;
};
struct hashtable
{
    int h[mod],p[mod],size;
    point s[mod];
    int hash(int x,int y)
    {
        return ((x*131+y+mod)&0x7FFFFFFF)%mod;
    }
    void insert(int x,int y)
    {
        int i,id=hash(x,y);
        for(i=h[id]; i>=0; i=p[i])
            if(s[i].x==x&&s[i].y==y)return;
        s[size].x=x,s[size].y=y;
        p[size]=h[id],h[id]=size++;
    }
    int find(int x,int y)
    {
        int i,id=hash(x,y);
        for(i=h[id]; i>0; i=p[i])
            if(s[i].x==x&&s[i].y==y)return i;
        return 0;
    }
    void clear()
    {
        size=1;
        memset(h,-1,sizeof(h));
    }
} ht;
bool check(point P,point Q)
{
    int add1=P.y-Q.y,add2=Q.x-P.x;
    return ht.find(P.x+add1,P.y+add2)&&ht.find(Q.x+add1,Q.y+add2);
}
int main()
{
    int n;
    point data[1005];
    while(~scanf("%d",&n),n)
    {
        ht.clear();
        for(int i=0; i<n; i++)
            scanf("%d%d",&data[i].x,&data[i].y),ht.insert(data[i].x,data[i].y);
        int ans=0;
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                if(j!=i&&check(data[i],data[j]))
                    ans++;
        printf("%d\n",ans/4);
    }
    return 0;
}
时间: 2024-09-07 18:20:36

POJ 2002 hash+枚举的相关文章

POJ 1873 凸包+枚举

题意:一片森林,每棵树有坐标,做成栅栏的长度,本身价值,让求砍下任意棵树做成栅栏将剩下的树围上,要求被砍的这些树价值和最小,价值相同时要求被砍下的树最少,输出被砍的树,和做完栅栏后剩余的长度. 这题就用位运算枚举,最高的复杂度不过2的15次方.1的状态表示被砍的,0的状态表示剩下的.需要注意的是凸包求的是三个点以上的,那么0个点,1个点,2个点的情况需要特判. #include <iostream> #include<cstdio> #include<cstring>

A题常用数据结构

  基本结构 高级结构 题单 集合结构   幷查集 POJ 1182 POJ 1308 POJ 1611 POJ 1986 POJ 1988 线性结构 数组 栈 队列 双端队列 POJ POJ POJ POJ POJ 树状结构 二叉树 BST AVL树 splay树(伸展树) Treap Cartesian Tree Size Balance Tree POJ 3580(splay tree) POJ 2761(Treap) POJ 2201(Cartesian Tree) POJ 3481(S

UVa 757 / POJ 1042 Gone Fishing: 枚举&amp;amp;贪心&amp;amp;想法题&amp;amp;优先队列

757 - Gone Fishing Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=698 http://poj.org/problem?id=1042 John is going on a fishing trip. He has h hours available ( ), and t

算法题:poj 3080 Blue Jeans(KMP匹配,枚举子串)

链接: http://poj.org/problem?id=3080 题目大意: 给出m(2<=m<=10)个DNA序列, 求出这m个序列中最长的公共字串.如果有多个相同长度的, 输出字典序最小的. 分析与总结: 依次枚举所有的子串, 然后再看是否在所有序列中都能够匹配.保存下长度最大且字典序最小的序 列. 代码: #include<iostream> #include<cstdio> #include<cstring> using namespace st

poj 3522 Slim Span:枚举+最小生成树

链接: http://poj.org/problem?id=3522 题目: Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4962   Accepted: 2587 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The grap

算法:poj 3140 Contestants Division(树形dp? dfs计数+枚举)

题目 给n个节点的带权树,删掉其中一边,就会变成两颗子树, 求删去某条边使得这这两颗子树的 权值之差的绝对值最小. 思路 直接dfs一次,计算所有子树的权值总和tot[i] 如果删掉一条边 (v, fa),fa是v的父亲节点, 那么v子树权值总和为tot[v],显然另一棵子树的权值总和就是sum-tot[v] , 最总取最小绝对值即可. 这题要注意用long long 其实就是dfs+枚举,想不通为什么有人会 把这题列为树形dp? 代码 /**==========================

算法:poj 2923 Relocation (枚举+背包 | 状态压缩+01背包)

题目大意: 有N件家具,每件的重量为(1 ≤ wi ≤ 100), 用两辆车把他们来运送到目的地.这 两辆车的限载重量分别为C1, C2(1 ≤ Ci ≤ 100) , 问最少几趟可以把所有家具运送到目的地. 这两辆车每趟都必须一起走,即使某辆车没载东西. 思路: (一) 先上自己的方法: 枚举第一辆车可能载的家具的所有组合情况,那么用二进制来表示状态则共有 1<<n ,如果 二进制的第i位上是1,那么就表示第i件家具是由第一辆车运的. 这样就相当于把所有家具分成了两 组,一组给第一辆车运,另

poj 2081 Recaman&amp;#39;s Sequence【hash】

题目意思不难理解就是第m个位置的数是根据第m-1位置的数推出来的如果a[m-1]-m>0,并且a[m-1]-m在前面的序列中没有出现过那么a[m] = a[m-1]-m否则a[m] = a[m-1]+m 另外唯一需要注意的一点就是hash数组开大一点. //打表 #include <stdio.h> #include <iostream> using namespace std; const int MAXN=500003; int a[MAXN]={0}; bool has

poj 1950 Dessert(dfs枚举,模拟运算过程)

/* 这个代码运行的时间长主要是因为每次枚举之后都要重新计算一下和的值! 如果要快的话,应该在dfs,也就是枚举的过程中计算出前边的数值(这种方法见第二个代码),直到最后,这样不必每一次枚举都要从头再算一遍值! */ #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; char ch[20]; char sign[3]={