Title Link: http://poj.org/problem?id=2002
Given a bunch of points, what points in these points can form a square, the topic given n<=1000, the direct enumeration of four points is sure to time out, so to do some optimization.
There are formulas, two points are known to be diagonally opposite the square, respectively (X1,Y1) and (X2,y2), then the other two points (X3,y3) and (X4,Y4) are divided into squares:
x3 = x2-(x2-= x2 + (x2-= x1-(x2-= y1 + (x2-x1)
Then we need to enumerate two points, and finally figure out which two points can be squared, and then see if there are two points in the remaining point set. I first sort, then do the two points to find, the problem can also be done with hash, through the hash can be in O (1) time to determine whether there is, more quickly. It is particularly important to note that if there is a square, then it is bound to enumerate the two sides of the polygon. This time you need to divide by 2.
1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> + - using namespacestd; + A Const intMAXN =1010; attypedefstructPoint { - intx, y; - Point () {} -Point (intXxintyy): X (xx), Y (yy) {} - }point; - intN, ans; in Point P[MAXN]; - to BOOLCMP (Point A, point B) { + if(a.x = = b.x)returnA.y <b.y; - returnA.x <b.x; the } * $ BOOLBsintXinty) {Panax Notoginseng intLL =0, rr =n, mm; - while(LL <=RR) { theMM = (ll + RR) >>1; + if(p[mm].x = = x && p[mm].y = = y)return 1; A Else if(CMP (P[MM], point (x, y))) ll = mm +1; the Elserr = mm-1; + - } $ return 0; $ } - - intMain () { the //freopen ("in", "R", stdin); - intX3, Y3, X4, Y4;Wuyi while(~SCANF ("%d", &n) &&N) { theAns =0; - for(inti =0; I < n; i++) { Wuscanf"%d%d", &p[i].x, &p[i].y); - } AboutSort (p, p+N, CMP); $ for(inti =0; I < n; i++) { - for(intj = i +1; J < N; J + +) { -x3 = p[j].x-(P[J].Y-p[i].y); -Y3 = P[j].y + (p[j].x-p[i].x); Ax4 = p[i].x-(P[J].Y-p[i].y); +Y4 = P[i].y + (p[j].x-p[i].x); the if(BS (x3, Y3) && BS (x4, Y4)) ans++; - } $ } theprintf"%d\n", ans/2); the } the return 0; the}
[POJ2002] Squares (computational geometry, two points)