POJ3739 Special Squares
Describe:
There is some points and lines parellel to x-axis or y-axis on the plane. If arbitrary chosen-lines parallel to x-axis and both lines parallel to y-axis, one rectangle, or sometimes a square, W Ill be formed. If a square is formed and there are one or more point in the square or on the side of the square, the square is called a "s Pecial Square ". Please find the number of special squares.
Input:
The 1st line contains three positive integer n1, N2 and N3. N1 stands for the number of lines parallel to x-axis, N2 stands for the number of lines parallel to y-axis, N3 stands for The number of points. (0<n1, N2, n3≤1000)
Each of the 2nd line to (n1+1) th line gives an integer y_i (0≤y_i≤1000), means a line with equation y=y_i.
Each of the (n1+2) th line to (n1+n2+1) th linegives an integer x_j (0≤x_j≤1000), means a line with equation X=x_j.
Each of the last three lines gives-integers px_k and Py_k (0≤px_k,py_k≤1000), means a point with coordinate (Px_k, py_k ).
Output:
Output oneline containing an integer specifies the number of special squares. The testdata ensures that the result was less than 2^31
Sample input:
4 4 30 2 4 60 2 4 61 13 36 6
Sample output
8
Topic Analysis:
The main idea is to enter some lines parallel to the x-axis, the y-axis, and some points to output the number of squares containing a bit of these lines.
This is a problem covered by the area. There are two difficulties in the subject: 1. How to judge and count all squares (not rectangles), 2. How to determine if these squares contain points.
My idea is to figure out all the squares and then walk through them to determine if they contain points.
The first step is to preprocess the point, the number of points between the point (0,0) point (I,J),
In the second step, the cross-ordinate values in the title are projected at 45 degrees to the x-axis, and the points are crossed by all the known straight lines, and stored in a vector of point groups.
The intersection point on the diagonal of the column i is stored in the point group Vector V[i].
Third, traverse the square to determine whether it contains points.
Since the points are stored in the point vector array V, traversing the points on these diagonals can form a square, as shown in:
The graph is a picture based on sample data, the blue dots represent the points given by the sample, the other parallel lines and their intersections, the intersections are indicated by red dots, a1...a14
Among them, the points group Vector v[1498] stored in point A1, a2;v[1499] stored in points A3, A4, A5 ... v[1502] in the storage of points A13, A14.
The fourth step is to use a diagonal line to indicate its diagonal line. The method of its containing point is, for example: A7a6 = A9a6-a9a10-a9a3 +a9a7. (A9 is the origin point, A6 is point 3)
The code is as follows:
1#include <stdio.h>2#include <vector>3#include <algorithm>4 using namespacestd;5 6 structpoint{7 intx;8 inty;9 };Ten OneVector<point> v[3002]; A intn1,n2,n3,x[ the],y[ the],p[ the][ the];//p[][] Count the number of points within the range - BOOLflag1[ the][ the]; -Point v1[ the]; the - intcmpvoid Const*a,void Const*b) - { - return*(int*) a-* (int*) b; + } - + intMain () A { at intI,J,K,LEN,CNT (0), flag,temp1,ii; - Point temp; - -scanf"%d%d%d",&n1,&n2,&n3); - for(i=0; i<n1;i++) scanf ("%d",&y[i]); - for(i=0; i<n2;i++) scanf ("%d",&x[i]); in for(i=0; i<n3;i++) - { toscanf"%d%d",&v1[i].x,&v1[i].y); +flag1[v1[i].x][v1[i].y]=1; - } the *Qsort (Y,N1,sizeof(int), CMP); $Qsort (X,N2,sizeof(int), CMP);Panax Notoginseng - for(i=0; i<n2; i++)//preprocessing a point the for(j=0; j<n1; J + +) + for(k=0; k<n3;k++) A if(V1[k].x<=x[i] && v1[k].y<=Y[j]) thep[x[i]][y[j]]++; + - for(i=0; i<n2; i++)//projecting the coordinates 45 degrees to the x-axis to get all the squares $ for(j=0; j<n1; J + +)//The points on the same diagonal are stored in 1 v[i], I means the diagonal of the line I $ { -len=x[i]-y[j]+ the; -temp.x=X[i]; thetemp.y=Y[j]; - V[len].push_back (temp);Wuyi } the for(i=0; i<3002; i++)//determine if the point is within a square - for(j=0; J<v[i].size (); J + +)//V[i][j] v[i][k] is the top right corner of the square left corner vertex Wu { -flag=0; About for(k=j+1; K<v[i].size (); k++) $ { -Ii=0; -Temp1 = P[v[i][k].x][v[i][k].y]-//Temp1 is the number of points inside the square -P[V[I][J].X][V[I][K].Y]- A P[V[I][K].X][V[I][J].Y]; + if(Flag1[v[i][j].x][v[i][k].y] = =1) ii++;//Judging (j.x, j.y) (j.x, K,y) (J.Y, k.x) Three points whether there is a bit of existence the if(Flag1[v[i][k].x][v[i][j].y] = =1) ii++; - if(Flag1[v[i][j].x][v[i][j].y] = =1) ii++; $ if(Temp1+p[v[i][j].x][v[i][j].y]+ii >0) the { theCnt+=v[i].size ()-K; theflag=1; the } - if(flag) Break; in } the } theprintf"%d\n", CNT); About return 0; the}
POJ3739 Special Squares Problem Solving report