The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
1: Understanding the Problem
Given an N×N region containing two types of points, J and B, add one J point (or do not add one; when adding, it cannot be placed where a B point already exists), and find the largest square consisting of J points.
2: Analysis
2.1: Summary
Because the data is small (N<100), and one J edge determines the other two edges of a square, we can enumerate J edges and check them to find the largest square.
2.2: A Little Mathematics
Suppose points P1 and P2 determine a line, and the vertical coordinate of P1 is always higher than that of P2. We can draw the following figure:
How do we calculate the coordinates of P3 and P4?
By observation, the four triangles in the figure are congruent. We only need to calculate the lengths of the long and short legs of the triangle (or the two different perpendicular legs; in this special case, the long and short legs are arranged as in the figure), then add an offset to obtain the coordinates of P3 and P4.
Long leg of the triangle:
L△=b1−b2
Short leg of the triangle:
S△=a1−a2
From these two formulas, we obtain the coordinates:
P3=(a2+L△,b2−S△)
P4=(a1+L△,b1−S△)
In addition to the arrangement shown, the line P1P2 determines another square:
Again, we can obtain the coordinates of P5 and P6 using the preceding method. For P3 and P4, we added or subtracted the two different triangle-leg lengths (L△ and S△) from an offset. By observation, applying the opposite operations to the offset gives P5 and P6:
P5=(a2−L△,b2+S△)
P6=(a1−L△,b1+S△)
Because we need the area of the square, calculate it as:
area=(a1−a2)2+(b1−b2)2
2.3: Program Idea
We can now obtain the coordinates of P3∼6. Next, determine whether the two squares determined by one edge are valid.
Because we may freely place one J point, a square may contain only three existing J points. The additional J point must be placed at an unoccupied point.
Therefore, the square made of P1∼4 is valid when:
(P3∈JANDP4∈/B)OR(P3∈/BANDP4∈J)
Note: J denotes the set of J points, and B denotes the set of B points.
The square made of P1,2,5,6 is handled similarly.
3: Implementation and Improvements
3.1: First Attempt
After thinking of the approach, I quickly wrote the code, but two test points timed out.
The code performs a great deal of useless calculation. We assumed that P1’s vertical coordinate is always higher than P2’s, so the program contains a check to ensure that assumption:
1
if (p1.y < p2.y) { swap(p1, p2); }
We enumerate all J-point pairs with two loops, so each edge can be enumerated twice in reverse order. We can skip such cases directly:
After this, every point entering the square-validity checks satisfies our assumption, and duplicate computation is reduced.
Further analysis shows that the loop only checks square validity. If the area of a square found during enumeration is no larger than the current answer, there is no need to check it, so skip it directly: