B-SquaresTime
limit:3500MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64u SubmitStatusPracticePOJ 2002
Description
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
41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20
Sample Output
161
First, provide a brute force enumeration, two-point solution, the code will be uploaded
/*author:2486memory:24256 kbtime:375 mslanguage:c++result:accepted*///This topic violent violence enumeration//through the already determined two points, calculated the remaining two points//(there are two cases)// One on top, one below # include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int MAXN =20000+5;struct point{int x, y;} Ps[1005];int n,ans;bool Vis[maxn<<1][maxn<<1];int Main () {while (~SCANF ("%d", &n), n) {ans=0; for (int i=0;i<n;i++) {scanf ("%d%d", &ps[i].x,&ps[i].y); ps[i].x+=20000,ps[i].y+=20000;//in the array can store negative numbers vis[ps[i].x][ps[i].y]=true;//mark this dot exists} for (int i=0;i<n;i++) { for (int j=0;j<i;j++) {if (i==j) continue;//represents the upper and lower two different square int nx1=ps[i].x+ps[i].y-ps[j].y respectively; int ny1=ps[i].y+ps[j].x-ps[i].x; int nx2=ps[j].x+ps[i].y-ps[j].y; int ny2=ps[j].y+ps[j].x-ps[i].x; if (Vis[nx1][ny1]&&vis[nx2][ny2]) ans++; nx1=ps[i].x-(PS[I].Y-PS[J].Y); ny1=ps[i].y-(ps[j].x-ps[i].x); nx2=ps[j].x-(PS[I].Y-PS[J].Y); ny2=ps[j].y-(ps[j].x-ps[i].x); if (Vis[nx1][ny1]&&vis[nx2][ny2]) ans++; }} for (int i=0;i<n;i++) {vis[ps[i].x][ps[i].y]=false;//must be zeroed, not memset, because the array is a bit large} printf ("%d\n", an S/4);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Squares-Violent enumeration or using binary optimization