POJ 2002 Squares (哈希)

题意:给定 n 个点,问有能组成多少个正方形。

析:通过直接桥梁两个顶点,然后再算另外两个,再通过哈希进行查找另外两个,这里我先是用的map,竟然卡过了3400ms多,后来改成哗哈希,900ms,哈希我也是用STL中的容器来写的,list,先枚举的那两个点是相邻的,然后再通过旋转90度,去计算另外两个。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 10;
const int mod = 99991;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
list<P> Hash[mod];
P a[maxn];

P solve(const P &lhs, const P &rhs){
  int detx = rhs.first - lhs.first;
  int dety = rhs.second - lhs.second;
  return P(lhs.first-dety, lhs.second+detx);
}

int calc(const P &p){
  return (p.first * p.first + p.second * p.second) % mod;
}

int main(){
  while(scanf("%d", &n) == 1 && n){
    for(int i = 0; i < mod; ++i)  Hash[i].clear();
    for(int i = 0; i < n; ++i){
      scanf("%d %d", &a[i].first, &a[i].second);
      Hash[calc(a[i])].push_back(a[i]);
    }
    int ans = 0;
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < n; ++j){
        if(i == j)  continue;
        P p3 = solve(a[i], a[j]);
        int x = calc(p3);
        bool ok = false;
        for(list<P> :: iterator it = Hash[x].begin(); it != Hash[x].end(); ++it)
          if(*it == p3){ ok = true;  break; }
        if(!ok)  continue;
        P p4 = solve(p3, a[i]);
        x = calc(p4);
        ok = false;
        for(list<P> :: iterator it = Hash[x].begin(); it != Hash[x].end(); ++it)
          if(*it == p4){ ok = true;  break; }
        if(!ok)  continue;
        ++ans;
      }
    }
    printf("%d\n", ans / 4);
  }
  return 0;
}

  

时间: 2024-11-13 11:55:35

POJ 2002 Squares (哈希)的相关文章

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

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 Squares 数学 + 必须hash

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

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 2002 Squares 计算集合 点的hash

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

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

E - Squares (POJ - 2002)

- 题目大意 有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少.相同的四个点,不同顺序构成的正方形视为同一正方形. - 解题思路 先枚举两个相邻的点,通过数学公式得到另外2个点,使得这四个点能够成正方形.然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形.但由于在计算过程中,点枚举了两次,因此最终结果需要除以2. - 代码 #include<iostream> #include<algorithm> using namespace std; str

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 Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 16908   Accepted: 6425 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