标签:
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 18493 | Accepted: 7124 |
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
题意:平面上有n个点,问n个点能够组成多少个正方形.
初看很简单,就是枚举每两个点去找另外两个点..然后我马上遇到了难题,我准备用bool去存点,结果试了一下 40000*40000直接爆掉了..然后正方形的其余两个点不会求..因为这题都是整形,用斜率求肯定
会出问题(平时做几何体,最好不要用斜率,因为会有正无穷)。然后竟然有公式!!用全等三角形可以证明。。。我等膜拜。。然后就是不能用bool只能用hash了。。hash又是一个好的借鉴。。链式前向星
存的。。
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <cmath> using namespace std; const int maxn = 1005; const int H = 10000; struct Point { int x,y; } p[maxn],C,D; ///************************hash模板 void initHash(){ memset(Hash,-1,sizeof(Hash)); cur =0; } struct Node { int x,y; int next; } node[maxn]; int Hash[H],cur; void InsertHash(int x,int y) { int h=(x*x + y*y) % H; node[cur].x=x; node[cur].y=y; node[cur].next=Hash[h]; Hash[h]=cur++; } bool SearchHash(int x,int y) { int h=(x*x + y*y) % H; int next=Hash[h]; while(next != -1) { if(node[next].x == x && node[next].y == y) return true; next = node[next].next; } return false; } /*******************************/ ///已知正方形 a,b 两点求解另外两点的 c,d的坐标(要分两种情况) void solve1(Point a,Point b,Point& c,Point& d) ///情况1 { c.x = a.x + (a.y-b.y); c.y = a.y - (a.x-b.x); d.x = b.x + (a.y-b.y); d.y = b.y - (a.x-b.x); } void solve2(Point a,Point b,Point& c,Point& d) ///情况2(反过来) { c.x = a.x - (a.y-b.y); c.y = a.y + (a.x-b.x); d.x = b.x - (a.y-b.y); d.y = b.y + (a.x-b.x); } int main() { int n; while(scanf("%d",&n)!=EOF&&n) { initHash(); for(int i=0; i<n; i++) { scanf("%d%d",&p[i].x,&p[i].y); InsertHash(p[i].x,p[i].y); } int ans = 0; for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { solve1(p[i],p[j],C,D); if(SearchHash(C.x,C.y)&&SearchHash(D.x,D.y)) ans++; solve2(p[i],p[j],C,D); if(SearchHash(C.x,C.y)&&SearchHash(D.x,D.y)) ans++; } } printf("%d\n",ans/4); } return 0; }
poj 2002(好题 链式hash+已知正方形两点求另外两点)
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5463461.html