Topic Links:
http://poj.org/problem?id=2002
Test instructions
A given n points to determine how many squares this n points can make.
Analysis:
Violence enumerates the two endpoints of an edge, and then, according to the congruent triangles, can find the endpoints of the other two sides of the square, and then judge that the two two exist.
So the first thing to do is to hash all the dots, then the brute force enumeration, so that the four sides are counted once, so the last one is to divide by 4.
The code is as follows:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < cmath>using namespace Std;const int mod = 10007;const int maxn = 1010;struct nod{int x, y;}; inline int read ()//input plug-in {char ch=getchar (); int x=0,f=1; while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') f=-1; Ch=getchar (); } while (ch<= ' 9 ' &&ch>= ' 0 ') {x=x*10+ch-' 0 '; Ch=getchar (); } return x*f;} struct hashmap{nod A[MAXN]; int head[mod],next[maxn],cnt; void Init () {cnt=0; memset (head,-1,sizeof (head)); memset (Next,0,sizeof (next)); } bool Find (nod val) {int tmp = (val.x*val.x+val.y*val.y)%mod; for (int i=head[tmp]; i!=-1; i=next[i]) if (A[I].X==VAL.X&&A[I].Y==VAL.Y) return true; return false; } void Add (nod val) {int tmp = (val.x*val.x+val.y*val.y)%mod; for (int i=head[tmp]; i!=-1; I=next[i]) if (A[I].X==VAL.X&&A[I].Y==VAL.Y) return; A[cnt]=val; NEXT[CNT]=HEAD[TMP]; head[tmp]=cnt++; }} H;nod P[maxn];int main () {int n; while (~SCANF ("%d", &n)} {if (n==0) break; H.init (); for (int i=0; i<n; i++) {p[i].x=read (); P[i].y=read (); H.add (P[i]); } int ans = 0; for (int i=0, i<n; i++) {for (int j=i+1; j<n; J + +) {nod tmp1,tmp2; tmp1.x = p[i].x-(P[I].Y-P[J].Y); TMP1.Y = P[i].y + (p[i].x-p[j].x); tmp2.x = p[j].x-(P[I].Y-P[J].Y); TMP2.Y = P[j].y + (p[i].x-p[j].x); if (H.find (TMP1) &&h.find (TMP2)) ans++; }} for (int i=0, i<n; i++) {for (int j=i+1; j<n; J + +) {No D TMP1,TMP2; tmp1.x = p[i].x + (P[I].Y-P[J].Y); TMP1.Y = P[i].y-(p[i].x-p[j].x); tmp2.x = p[j].x + (P[I].Y-P[J].Y); TMP2.Y = P[j].y-(p[i].x-p[j].x); if (H.find (TMP1) &&h.find (TMP2)) ans++; }} ans >>= 2; printf ("%d\n", ans); } return 0;}
POJ2002 squares (two-dimensional point hash)