http://poj.org/problem?id=2002
Can only say that the hash is much faster than the binary. A random hash function can be finished two points.
Determine if there is a square idea as follows:
1, enumerate any two points, as a square side, then the whole square is determined, there are two directions.
Because
The coordinates of the enumeration are (x1, y1) & (x2, y2), and the coordinates are connected with the x1,y1 point, then there are the equations as follows.
1, vertical, vector product is 0
2. The side length is equal, then the distance is formulated simply.
You can solve the remaining two points.
Then pay attention to two points on the same side of the square, or change the parallelogram.
Awakened my memory of the hash.
First hash, you need to translate the coordinates first, because to avoid negative conditions, so the translation of 40,000 units. And then make a simple hash.
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<assert.h>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN =10007+ -;structNode {intx, y; BOOL operator< (Const structNode & RHS)Const { if(X! =rhs.x) {returnX <rhs.x; } Else returnY <Rhs.y; } BOOL operator!= (Const structNode & RHS)Const { return! (x = = Rhs.x && y = =rhs.y); }} arr[2222];intN;Const intMOD =10007;structEdge {intVal, id, Tonext;} E[MAXN*2];intFIRST[MAXN];intnum;voidAddintValintID) {++num; intTval =Val; Val%=MOD; E[num].id=ID; E[num].val=Tval; E[num].tonext=First[val]; First[val]=num;}BOOLTofind (intValintOneintBoth ) {Assert (Val>=0); intTval =Val; Val%=MOD; for(inti = First[val]; I i =E[i].tonext) { if(E[i].val = = Tval && e[i].id! = one && e[i].id! =)return true; } return false;}BOOLCheckstructNode T1,structNode T2,intOneintBoth ) { returnTofind (t1.x *20001+ T1.y, one, both) &&Tofind (t2.x*20001+T2.y, one, and both);}voidWork () {//cout << (20000 + 20000) * 20001 << Endl;num =0; memset (First,0,sizeofFirst ); for(inti =1; I <= N; ++i) {scanf ("%d%d", &arr[i].x, &arr[i].y); arr[i].x+=40020; Arr[i].y+=40020; Add (arr[i].x*20001+arr[i].y, i); ASSERT (arr[i].x*20001+ Arr[i].y >=0); }//cout << tofind (arr[1].x * 20001 + ARR[1].Y, 3, 2) << Endl;//Sort (arr + 1, arr + 1 + N);//for (int i = 1; I <= n; ++i) {//cout << arr[i].x << "<< arr[i].y << Endl;// }//cout << Endl;LL ans =0; for(inti =1; I <= N; ++i) { for(intj = i +1; J <= N; ++j) {structNode t[8]; t[0].x = Arr[i].y-arr[j].y +arr[i].x; t[0].Y = arr[j].x-arr[i].x +arr[i].y; t[1].x = Arr[j].y-arr[i].y +arr[j].x; t[1].Y = arr[i].x-arr[j].x +arr[j].y;//cout << i << "" << J << Endl;//cout << "*********" << Endl;//for (int k = 0; k <= 1; ++k) {//cout << t[k].x << "<< t[k].y << Endl;// }//cout << "**********" << Endl; if(t[0].x < arr[i].x | | t[0].x = = arr[i].x && t[0].y <arr[i].y) {t[0].x = Arr[j].y-arr[i].y +arr[i].x; t[0].Y = arr[i].x-arr[j].x +arr[i].y; } if(t[1].x < arr[j].x | | t[1].x = = arr[j].x && t[1].y <arr[j].y) {t[1].x = Arr[i].y-arr[j].y +arr[j].x; t[1].Y = arr[j].x-arr[i].x +arr[j].y; } if(Check (t[0], t[1], I, j) + +ans; t[0].x = Arr[i].y-arr[j].y +arr[i].x; t[0].Y = arr[j].x-arr[i].x +arr[i].y; t[1].x = Arr[j].y-arr[i].y +arr[j].x; t[1].Y = arr[i].x-arr[j].x +arr[j].y; if(t[0].x > Arr[i].x | | t[0].x = = arr[i].x && t[0].y >arr[i].y) {t[0].x = Arr[j].y-arr[i].y +arr[i].x; t[0].Y = arr[i].x-arr[j].x +arr[i].y; } if(t[1].x > Arr[j].x | | t[1].x = = arr[j].x && t[1].y >arr[j].y) {t[1].x = Arr[i].y-arr[j].y +arr[j].x; t[1].Y = arr[j].x-arr[i].x +arr[j].y; }//for (int k = 0; k <= 1; ++k) {//cout << t[k].x << "<< t[k].y << Endl;// }//cout << Endl; if(Check (t[0], t[1], I, j) ans++; }} assert (Ans>=0); printf ("%lld\n", ans/4); return;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);//freopen ("Data.txt", "w", stdout);#endif while(SCANF ("%d", &n)! = EOF &&N) work (); return 0;}
View Code
POJ 2002 Squares Math + must hash