To solve this problem, we need to determine whether there's a path from the top-left corner toward the bottom-right corner in a rectangle filled with circular obstacles. Importantly, the movement is restricted to not touching any of these obstacles or the rectangle's boundary.
Here's a high-level solution outline:
-
Modeling the Problem: We treat the circles and sides of the rectangle as nodes in a graph. The circles can be connected, and they can also connect to the sides of the rectangle if they touch or cross the boundaries (since movement cannot pass through such points).
-
Union-Find Data Structure (Disjoint Set Union): We use this data structure to keep track of connected components. Each circle is initially its own set. If two circles touch or overlap, they are merged (unioned) into the same set.
-
Connecting Circles with Rectangle Sides:
- For each circle, check if it touches any side of the rectangle (top, bottom, left, right). If it does, merge the circle's set with that side's set.
- This process accounts for larger obstacles that may block movement across the rectangle by connecting multiple circles into a single blocking component.
-
Determining the Blockage:
- After processing the unions, we check if any of the following pairs are in the same set:
- Left and Right sides: If they are, it means there's a vertical blockage, and the rectangle is split horizontally. This would prevent movement from the left to the right.
- Top and Bottom sides: If they are, it means there's a horizontal blockage, and the rectangle is split vertically. Movement from top to bottom is impossible.
-
Conclusion:
- If neither the left-right nor the top-bottom sides are connected through circles, movement from the start to the goal is possible ("YES").
- If either pair is connected, then movement is blocked ("NO").
-
Implementation Details:
- Use efficient computations for circle overlaps and unions since we have to process potentially up to a million circle pairs.
- Implement union-find with path compression to optimize the merging of sets.
Now, here's the complete code that implements the above solution:
import sys
import threading
def main():
T = int(sys.stdin.readline())
for _ in range(T):
line = ''
while line.strip() == '':
line = sys.stdin.readline()
X_str, Y_str = line.strip().split()
X, Y = float(X_str), float(Y_str)
N_line = ''
while N_line.strip() == '':
N_line = sys.stdin.readline()
N = int(N_line)
circles = []
for _ in range(N):
while True:
circle_line = sys.stdin.readline()
if circle_line.strip() == '':
continue
else:
break
cxi_str, cyi_str, ri_str = circle_line.strip().split()
cxi, cyi, ri = float(cxi_str), float(cyi_str), float(ri_str)
circles.append((cxi, cyi, ri))
# Union-Find Data Structure
parent = [i for i in range(N + 4)] # N circles + 4 sides
def find(u):
while parent[u] != u:
parent[u] = parent[parent[u]]
u = parent[u]
return u
def union(u, v):
pu, pv = find(u), find(v)
if pu != pv:
parent[pu] = pv
LEFT, RIGHT, BOTTOM, TOP = N, N+1, N+2, N+3
# For each circle, determine which sides it touches
for i, (cx, cy, r) in enumerate(circles):
if cx - r <= 0:
union(i, LEFT)
if cx + r >= X:
union(i, RIGHT)
if cy - r <= 0:
union(i, BOTTOM)
if cy + r >= Y:
union(i, TOP)
# For each pair of circles, check if they touch or overlap
for i in range(N):
cxi, cyi, ri = circles[i]
for j in range(i+1, N):
cxj, cyj, rj = circles[j]
dx = cxi - cxj
dy = cyi - cyj
dist_sq = dx*dx + dy*dy
radii_sum = ri + rj
if dist_sq <= radii_sum * radii_sum:
union(i, j)
# Check if LEFT and RIGHT sides are connected
left_right_connected = find(LEFT) == find(RIGHT)
top_bottom_connected = find(TOP) == find(BOTTOM)
if left_right_connected or top_bottom_connected:
print("NO")
else:
print("YES")
threading.Thread(target=main).start()