Cheese Way
There is a large block of cheese with a height of $h$. We can assume its length and width are infinitely large. Inside this cheese, there are many spherical holes of the same radius. We can establish a spatial coordinate system within this cheese, where the bottom surface of the cheese is at $z=0$ and the top surface of the cheese is at $z=h$.
Currently, there is a little mouse named Jerry on the bottom surface of the cheese. Jerry knows the coordinates of the centers of all the holes in the cheese. If two holes are tangent or intersect, Jerry can move from one hole to another. Specially, if a hole is tangent to or intersects the bottom surface, Jerry can enter the hole from the bottom surface of the cheese; if a hole is tangent to or intersects the top surface, Jerry can exit from the hole to the top surface of the cheese.
Jerry, located on the bottom surface of the cheese, wants to know if he can utilize the existing holes to reach the top surface of the cheese without damaging the cheese.
Each input stream contains varies of data.
In the first line, it includes an integer $T$ which indicates the total number of input data lines. Then it followed by $T$ lines of data, each line is formatted as $n,h,r$, separated by space
, representing number of holes, height of cheese, and the radius of holes. The following $n$ lines contains three integers $x,y,z$, separated by space
, representing the coordinate of each hole.
Solution
1 | class Cheese { |
Explanation:
This is a typical Merge-find problem. We can split the holes into two sets: those whom connected to the bottom surface and those whom connected to the top surface. If a hole tangent to or intersects the bottom surface, then merge it into the bottom set, similarly, we can merge those whom are connected to the top surface to the top set. To determine whether there are holes can form a pathway, we just need to know whether the top surface and the bottom surface are in the same set. All above can be achieved by using merge-find algorithm.