poj2002 hash
来源:互联网 发布:js shift 多选 编辑:程序博客网 时间:2024/06/02 01:58
建立一张hash表,将所有的顶点加入该表
枚举正方形相邻两个顶点,求出另外两个顶点的坐标,判断这两个顶点是否在hash表中。
#include <iostream>#include <cstdio>using namespace std;#define TABLE_SIZE 2003int p[1005][2];struct Node { int x, y; Node *next;} table[TABLE_SIZE];int hash (int x, int y){ return (x * x + y * y) % TABLE_SIZE;}void insert (int x, int y) { int key = hash(x, y); Node * newNode = new Node; newNode->x = x; newNode->y = y; newNode->next = table[key].next; table[key].next = newNode;}bool search (int x, int y) { Node * cur = table[hash(x, y)].next; while (cur != NULL) { if (cur->x == x && cur->y == y) return true; cur = cur->next; } return false;}int main(){ int n, i, j; while (scanf ("%d", &n) && n) { for (i = 0; i < TABLE_SIZE; i++) table[i].next = NULL; for (i = 0; i < n; i++) { scanf ("%d %d", &p[i][0], &p[i][1]); insert (p[i][0], p[i][1]); } int x1, y1, x2, y2, ans = 0; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (i == j) continue; x1 = p[i][0] + p[i][1] - p[j][1]; y1 = p[i][1] - p[i][0] + p[j][0]; x2 = p[j][0] + p[i][1] - p[j][1]; y2 = p[j][1] - p[i][0] + p[j][0]; if (search(x1, y1) && search(x2, y2)) ans++; x1 = p[i][0] - p[i][1] + p[j][1]; y1 = p[i][1] + p[i][0] - p[j][0]; x2 = p[j][0] - p[i][1] + p[j][1]; y2 = p[j][1] + p[i][0] - p[j][0]; if (search(x1, y1) && search(x2, y2)) ans++; } } printf ("%d\n", ans / 4); for (i = 0; i < TABLE_SIZE; i++) { while (table[i].next != NULL) { Node * tmp = table[i].next; table[i].next = tmp->next; delete tmp; } } } return 0;}
- poj2002 hash
- poj2002 数正方形 hash
- POJ2002 Squares(hash)
- poj2002 Squares(hash)
- POJ2002-Squares-hash+枚举+几何
- POJ2002&&POJ3432 Squares(二分||hash)
- 二分|hash(找正方形)poj2002
- poj2002——Squares(hash)
- POJ2002
- poj2002
- poj2002
- poj2002
- POJ2002
- poj2002
- POJ2002 Squares
- poj2002 Squares
- poj2002 Squares
- POJ2002-Squares
- Android 简单异步处理类------实现声音的录制及播放
- Java SE 6 生命将在今年11月终结
- Project interpreter not specified(eclipse+pydev)
- linux 开机自启脚本
- 随机产生用户名
- poj2002 hash
- 如何由IP地址反向查询自治域号
- qt移植出现Segmentation fault
- MYGUI Generic factory
- jQuery 循环图片滚动切换效果代码
- 在实现一个WIFI无线连接设置中需要注意的问题
- java.util.concurrent-------TimeUnit
- C#中调用非托管的DLL及参数传递 (一)
- 网摘