题目:
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 15261 | Accepted: 5792 |
Description
Input
Output
Sample Input
4 1 0 0 1 1 1 0 0 9 0 0 1 0 2 0 0 2 1 2 2 2 0 1 1 1 2 1 4 -2 5 3 7 0 0 5 2 0
Sample Output
1 6 1
Source
1 struct point 2 { 3 int x; 4 int y; 5 }poin[maxn]; 6 7 int hash[maxn+10]; 8 int next[maxn+10]; 9 10 int findkey(point p) 11 { 12 return abs(p.x+p.y)%maxn; 13 } 14 15 void hashinsert(int i) 16 { 17 int key=findkey(poin[i]); 18 next[i]=hash[key];//最后一个next储存的是-1,其他的都指向下一个next 19 hash[key]=i;//指向最开头的next的下标 20 } 21 22 int hashsearch(point p) 23 { 24 int key=findkey(p); 25 int i=hash[key]; 26 while(i!=-1) 27 { 28 if(p.x==poin[i].x&&p.y==poin[i].y)//找到一个这样的点 29 return 1; 30 i=next[i]; 31 } 32 return 0; 33 }
题目代码:
1 #include<iostream> 2 #include<string.h> 3 #include<stdio.h> 4 #include<algorithm> 5 #include<math.h> 6 using namespace std; 7 8 const int maxn=1031; 9 10 struct point 11 { 12 int x; 13 int y; 14 }poin[maxn]; 15 16 int hash[maxn+10]; 17 int next[maxn+10]; 18 19 int findkey(point p) 20 { 21 return abs(p.x+p.y)%maxn; 22 } 23 24 void hashinsert(int i) 25 { 26 int key=findkey(poin[i]); 27 next[i]=hash[key]; 28 hash[key]=i; 29 } 30 31 int hashsearch(point p) 32 { 33 int key=findkey(p); 34 int i=hash[key]; 35 while(i!=-1) 36 { 37 if(p.x==poin[i].x&&p.y==poin[i].y) 38 return 1; 39 i=next[i]; 40 } 41 return 0; 42 } 43 44 45 bool cmp(point p1,point p2) 46 { 47 if(p1.x!=p2.x) 48 return p1.x<p2.x; 49 else 50 return p1.y<p2.y; 51 } 52 53 int main() 54 { 55 int n; 56 int x1,x2,x3,x4; 57 int y1,y2,y3,y4; 58 //freopen("aa.txt","r",stdin); 59 while(scanf("%d",&n)!=EOF) 60 { 61 if(n==0) 62 break; 63 memset(hash,-1,sizeof(hash)); 64 memset(next,-1,sizeof(next)); 65 point p3,p4; 66 for(int i=0;i<n;i++) 67 scanf("%d%d",&poin[i].x,&poin[i].y); 68 sort(poin,poin+n,cmp); 69 for(int i=0;i<n;i++) 70 hashinsert(i); 71 int ans=0; 72 for(int i=0;i<n;i++) 73 { 74 for(int j=i+1;j<n;j++) 75 { 76 x1=poin[i].x; 77 y1=poin[i].y; 78 x2=poin[j].x; 79 y2=poin[j].y; 80 p3.x=x1+(y2-y1); 81 p3.y=y1-(x2-x1); 82 p4.x=x2+(y2-y1); 83 p4.y=y2-(x2-x1); 84 if(hashsearch(p3)&&hashsearch(p4)) 85 ans++; 86 } 87 } 88 printf("%d\n",ans/2); 89 } 90 return 0; 91 }
poj2002 -- 点的hash,布布扣,bubuko.com
原文:http://www.cnblogs.com/zhanzhao/p/3588496.html