Links: Http://poj.org/problem?id=2002Squares
Time Limit: 3500MS |
|
Memory Limit: 65536K |
Total Submissions: 21720 |
|
Accepted: 8321 |
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
Let's leave a few pits:
① is said to have a two-part approach, which accounts for more pit-waiting;
② started with a vector, t ... Turn the vector into an array, then optimize the constant, occupy the pit for more
Main topic:
There are N (1<=n<=1000) points in the plane, coordinates of the given point xi,yi, how many squares can be formed altogether
Analysis: According to the relationship between the relations, from which two points a, a and two points to determine the m,n, see if m,n exists, determine the time complexity of two points is O (N2), the time complexity of the lookup is O (logn), the overall O (2N2LOGN) data range within 1000, acceptable
Start with the vector match, determine the N2 bar vector, and then determine the starting point and size of a vector, direction (parallel), vector matching, and then T (Pit ①) (there is a (wrong) idea, two vectors perpendicular, but this can only determine three points, no longer think) and then based on two points to match the other two points , 900+ms,a dropped (it seems that the constant is not written)
On the code:
1#include <iostream>2#include <cstdio>3#include <queue>4#include <cstdio>5#include <cstring>6#include <cstdlib>7 #defineMem (A, B) memset (A,b,sizeof (a))8 using namespacestd;9 Ten Const intMod=100007; One Const intmaxn=120009; A structnode - { - intX,y,next; the }EDGE[MAXN]; - intCNT; - intANS,HEAD[MAXN]; - intxi[1111],yi[1111]; +InlineintRead () - { + Charch = getchar ();intx =0, F =1; A while(Ch <'0'|| CH >'9') {if(ch = ='-') F =-1; CH =GetChar ();} at while('0'<= CH && Ch <='9') {x = x *Ten+ CH-'0'; CH =GetChar ();} - returnX *F; - } - void out(intA//instead of printf, faster, commonly known as output hanging - { - if(A >9) in { - out(ATen); to } +Putchar (a%Ten+'0'); - } the voidInsintXintYinth) * { $h%=MoD;Panax Notoginsengedge[cnt].x=x; -edge[cnt].y=y; theedge[cnt].next=Head[h]; +head[h]=cnt++; A } theInlineBOOLCMP (node A,node b) + { - if(A.X==B.X&&A.Y==B.Y)return 1; $ return 0; $ } - BOOLSearch (Node A,inth) - { theh%=MoD; - //printf ("search:\n");Wuyi for(inti=head[h];i!=-1; i=edge[i].next) the { - //"%d%d%d%d%d%d%d%d%d%d%d %d\n", xi[i],yi[i],xi[j],yi[j],a.x,a.y,b.x,b.y,c.x,c.y,d.x,d.y); Wu if(CMP (EDGE[I],A))return 1; - } About return 0; $ } - voidInit () - { -Mem (Edge,0); AMEM (head,-1); +Mem (xi,0); theMem (Yi,0); -Cnt=0; $ans=0; the } the intMain () the { the intN; - while(SCANF ("%d", &n) &&N) in { the init (); the for(intI=1; i<=n;i++) About { theXi[i]=read (); yi[i]=read (); theIns (Xi[i],yi[i],abs (xi[i]* ++yi[i])); the } + for(intI=1; i<=n;i++) - { the for(intj=i+1; j<=n;j++)Bayi { the intdty=yi[j]-yi[i],dtx=xi[j]-Xi[i]; the node a,b,c,d; -a.x=xi[i]+dty;a.y=yi[i]-DTX; -b.x=xi[j]+dty;b.y=yi[j]-DTX; thec.x=xi[i]-dty;c.y=yi[i]+DTX; thed.x=xi[j]-dty;d.y=yi[j]+DTX; the intHa=abs (a.x* ++A.Y), Hb=abs (b.x* ++b.y), theHc=abs (c.x* ++C.Y), Hd=abs (d.x* ++d.y); - //printf ("%d%d%d%d%d%d%d%d\n%d%d%d ", xi[i],yi[i],xi[j],yi[j],a.x,a.y,b.x,b.y,c.x,c.y,d.x,d . y); the if(Search (A,ha) &&search (B,HB)) {ans++;} the if(Search (C,HC) &&search (D,HD)) {ans++;} the }94 } the out(ans/4);p Utchar ('\ n'); the } the}
View Code
poj2002 (very simple math/geometry +hash)