码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2002 Squares (哈希)

时间:2017-07-22 23:43:19      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:pac   can   clu   mod   90度   std   string   cto   efi   

题意:给定 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;
}

  

POJ 2002 Squares (哈希)

标签:pac   can   clu   mod   90度   std   string   cto   efi   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7223011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!