Topic Connection
http://poj.org/problem?id=2002
Squaresdescription
A square is a 4-sided polygon whose sides has equal length and adjacent sides form 90-degree angles. It is also a polygon such the degrees gives the same polygon of It centre by. It isn't the only polygon with the latter property, however, as a regular octagon also have this property.
So we are know what's a square looks like, but can we find all possible squares that can is formed from a set of stars in a Night sky? To make the problem easier, we'll assume that the night sky was a 2-dimensional plane, and each star was specified by its X and Y coordinates.
Input
The input consists of a number of test cases. Each test case is starts with the integer n (1 <= n <=) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (both integers) of each point. Assume that the points is distinct and the magnitudes of the coordinates is less than 20000. The input is terminated when n = 0.
Output
For each test case, print on a line the number of squares one can form from the given stars.
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
3 T
0
Sample Output
1
6
1
Test instructions: Give a set of points to make the number of squares.
idea: Direct enumeration of words $n = 1000\ \ O (n^4) $ will time out. Consider a different idea: two points are known, and the geometric properties of the squares
find the coordinates of the other two points to determine whether in the set of points (hash, two, set ...). ) Can I use the hash.
specific to all the points at the horizontal axis, ordinate from small to large sort. The number of statistics $tot$, the final answer is ${tot/2}$ (the symmetry of the square)
so the question is, how do you know two points and ask for two other points?
now give the formula: Record $ A (x_1,y_1) \ \ \ B (x_2,y_2) \ \ \overrightarrow {AB} = (x_2-x_1,y_2-y_1) $
the other two points $c (x_3, y_3) \ \ \ D (x_4,y_4) \ \ \ $
$C: \ \ \ x_3 = y_1-y_2 + x_1\ \ y_3 = x_2-x_1 + y_1 $
$D: \ \ \ x_4= y_1-y_2 + x_2\ \ y_4 = x_2-x_1 + y_2 $
proof is actually very simple remember $\overrightarrow{x} = (A, b) $ counterclockwise rotation $\beta$ degrees get $\overrightarrow{y} (x, y) $
there are: $x = acos\beta-bsin\beta\ \ y = asin\beta+bcos\beta$ (the geometric meaning of trigonometric functions is easy to get)
so $\overrightarrow {AC} = (x_3-x_1,y_3-y_1) $
$x _3-x_1= (x_2-x_1) Cos\beta-(y_2-y_1) sin\beta$
$y _3-y_1= (x_2-x_1) Sin\beta + (y_2-y_1) cos\beta$ where $\beta = 90^0$
so $x_3 = y_1-y_2+x_1\ \ y_3=x_2-x_1+y_1$
$\overrightarrow {bd}$ (note the direction of the vector and the direction of rotation)
forgive my weak language level written too bad to see it/(ㄒoㄒ)/~~
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <map>using std::map;using std::abs;using std::sort;using std::p air; Using std::vector;using std::multimap; #define PB (E) push_back (e) #define SZ (c) (int) (c). Size () #define MP (A, b) make_pair (A, B) #define ALL (c) (c). Begin (), (c). End () #define ITER (c) __typeof ((c). Begin ()) #define CLS (arr, Val) memset (arr, Val, si Zeof (arr)) #define Cpresent (C, E) (Find (All (c), (e))! = (c). End ()) #define REP (i, n) for (int i = 0; i < (int) n; i++) #defi NE tr (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) const int N = 40007;const int INF = 0x3f3f3f3f;struct P { int x, y; P () {} p (int i, int j): X (i), Y (j) {} inline bool operator< (const P &k) Const {return x = = k.x? Y & Lt K.y:x < k.x; }}a[n];struct hash_set {int tot, head[n]; struct Edge {int x, y, next;} G[n]; inline void init () {tot = 0, cls (Head,-1); } inline void Insert (P &k) {int u = ABS (k.x + k.y)% N; G[tot] = (edge) {k.x, k.y, Head[u]}; Head[u] = tot++; } inline bool Find (P k) {int u = ABS (k.x + k.y)% N; for (int i = head[u]; ~i; i = g[i].next) {Edge &e = G[i]; if (k.x = = e.x && k.y = = e.y) return true; } return false; }}hash;int Main () {#ifdef LOCAL freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w+", stdout); #endif int n, x1, x2, x3, x4, y1, y2, y3, y4, ans; while (~SCANF ("%d", &n), N) {hash.init (); Rep (i, N) {scanf ("%d%d", &a[i].x, &A[I].Y); Hash.insert (A[i]); } sort (A, a + N); Ans = 0; for (int i = 0; i < n; i++) {for (int j = i + 1; j < N; j + +) {x1 = a[i].x, y1 = a[i].y; x2 = a[j].x, y2 = a[j].y; x3 = y1-y2 + x1, y3 = x2-x1 + y1; x4 = Y1-y2 + x2, y4 = x2-x1 + y2; if (Hash.find (P (x3, y3)) && Hash.find (P (x4, Y4))) ans++; }} printf ("%d\n", ans >> 1); } return 0;}
Poj 2002 Squares