POJ3432《Count Squares》方法:哈希
2013-04-05 10:52
120 查看
题目大意:跟POJ2002一摸一样,就数据由1000变为2000,找正方形形。
解题思路:已知: (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)
解题思路:已知: (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)
// 304k 297ms #include <iostream> #include <cstring> using namespace std; const int prime = 1999; typedef class Node { public: int x, y; } Node; Node node[2000]; typedef class HashTable { public: int x, y; HashTable *next; HashTable() { next = NULL; } } HashTable; HashTable *hashs[prime]; void insert_hash(int x, int y) { int key = (x*x + y*y) % prime; if (!hashs[key]) { HashTable *temp = new HashTable; temp->x = x; temp->y = y; hashs[key] = temp; } else { HashTable *temp = hashs[key]; while (temp->next) { temp = temp->next; } temp->next = new HashTable; temp->next->x = x; temp->next->y = y; } } bool find(int x, int y) { int key = (x*x + y*y) %prime; if (!hashs[key]) { return false; } else { HashTable *temp = hashs[key]; while (temp) { if (temp->x == x && temp->y == y) { return true; } temp = temp->next; } } return false; } int main() { //freopen("temp.txt", "r", stdin); int n, num; while (cin >> n && n != 0) { memset(hashs, 0, sizeof(hashs)); num = 0; for (int i = 0; i < n; ++i) { cin >> node[i].x >> node[i].y; insert_hash(node[i].x, node[i].y); } int x1, x2, x3, x4, y1, y2, y3, y4, a, b; for (int i = 0; i < n-1; ++i) { for (int j = i+1; j < n; ++j) { x1 = node[i].x; y1 = node[i].y; x2 = node[j].x; y2 = node[j].y; a = x1 - x2; b = y1 - y2; x3 = x1 + b; y3 = y1 - a; x4 = x2 + b; y4 = y2 - a; if (find(x3, y3) && find(x4, y4)) { ++num; } x3 = x1 - b; y3 = y1 + a; x4 = x2 - b; y4 = y2 + a; if (find(x3, y3) && find(x4, y4)) { ++num; } } } cout << num/4 << endl; } return 0; }
相关文章推荐
- (转)局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- ruby 哈希常用方法
- 局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- 解决哈希(HASH)冲突的主要方法
- 局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- 局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- 局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- 解决哈希(HASH)冲突的主要方法
- 哈希长度扩展攻击(Hash Length Extension Attack)利用工具hexpand安装使用方法
- 局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- PTA 7-15(查找) 航空公司VIP客户查询(25 分) 25分代码 哈希方法
- 局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- 局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- 哈希冲突的解决方法
- 查找树与哈希方法学习
- PHP中创建和验证哈希的简单方法实探
- POJ1840《Eqs》方法:哈希
- 【经典算法】:哈希方法
- 处理哈希(HASH)冲突的方法
- 局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍