POJ 2002
2012-06-11 15:49
113 查看
哈希技术
#include <stdio.h> #include <stdlib.h> #include <memory.h> #define maxn 1003 typedef struct { int x; int y; }Node; Node sq[1003]; typedef struct THashTable{ int x,y; THashTable* next; }HashTable; HashTable* hash[maxn]; void InsertHT(int i)//构建哈希表 { int key=(sq[i].x)*(sq[i].x)+(sq[i].y)*(sq[i].y);//关键字key int temH=key%maxn; if(!hash[temH]) { HashTable* tem=(HashTable*) malloc(sizeof(HashTable));//易忘点 tem->x=sq[i].x; tem->y=sq[i].y; tem->next=0; hash[temH]=tem; } else { HashTable* tem=hash[temH]; while(tem->next) { tem=tem->next; } tem->next=(HashTable*) malloc(sizeof(HashTable));//不要忘了这一步 tem->next->x=sq[i].x; tem->next->y=sq[i].y; tem->next->next=0; } } int find(int xx,int yy) { int key=xx*xx+yy*yy;//关键字key int temH=key%maxn; if(!hash[temH]) return 0;//就没有这个同义词 HashTable* tem=hash[temH]; while(tem)//易错点 { if(tem->x==xx&&tem->y==yy) return 1; tem=tem->next; } return 0; } int main() { int n; while(scanf("%d",&n)&&n!=0) { memset(hash,0,sizeof(hash)); int i; int tot=0; for(i=0;i<n;i++) { scanf("%d %d",&sq[i].x,&sq[i].y); InsertHT(i); } int x3,y3,x4,y4; for(i=0;i<(n-1);i++) { for(int j=i+1;j<n;j++) { x3=sq[i].x+(sq[i].y-sq[j].y); y3=sq[i].y-(sq[i].x-sq[j].x); x4=sq[j].x+(sq[i].y-sq[j].y); y4=sq[j].y-(sq[i].x-sq[j].x); if(find(x3,y3)&&find(x4,y4)) tot++; x3=sq[i].x-(sq[i].y-sq[j].y); y3=sq[i].y+(sq[i].x-sq[j].x); x4=sq[j].x-(sq[i].y-sq[j].y); y4=sq[j].y+(sq[i].x-sq[j].x); if(find(x3,y3)&&find(x4,y4)) tot++; } } tot=tot/4; printf("%d\n",tot); } return 0; }
相关文章推荐
- POJ 2002 Squares 笔记
- POJ 2002 Squares(二分)
- poj 2002 Squares
- poj 2002 -- hash
- POJ 2002.Squares
- POJ-2002-Squares
- POJ 2002 Squares
- hash poj2002
- POJ 2002 检查正方形
- poj 2002 Squares
- poj 2002 Squares【HASH】POJ最快
- POJ 2002 Squares
- 【poj 1330】 Nearest Common Ancestors 【Taejon 2002】
- poj_2002 哈希
- poj 2002 :Squares(hash 不解释)
- Poj2002 Squares
- poj2002 Squares
- poj 2002 Squares
- poj2002——Squares(hash)
- POJ 2002 Squares Hash