To determine whether there exists a path from the bottom-left corner ((0, 0)) to the top-right corner ((xCorner, yCorner)) of the rectangle that avoids all given circles, we can use a Union-Find (Disjoint Set Union) approach to identify connected components of overlapping circles. The key idea is to check whether any connected group of circles forms a barrier that spans from one side of the rectangle to the opposite side, effectively blocking any possible path.
Steps to Solve the Problem:
-
Identify Circle Boundaries:
- Left Boundary: A circle touches the left boundary if its center's x-coordinate minus its radius is less than or equal to 0.
- Right Boundary: A circle touches the right boundary if its center's x-coordinate plus its radius is greater than or equal to
xCorner.
- Bottom Boundary: A circle touches the bottom boundary if its center's y-coordinate minus its radius is less than or equal to 0.
- Top Boundary: A circle touches the top boundary if its center's y-coordinate plus its radius is greater than or equal to
yCorner.
-
Union-Find Structure:
- Initialization: Each circle is initially in its own set.
- Union Operation: Iterate through all pairs of circles and unite them if they overlap. Two circles overlap if the distance between their centers is less than or equal to the sum of their radii.
- Track Boundary Touches: For each connected component (set of circles), track if they touch any of the four boundaries.
-
Determine if a Barrier Exists:
- After forming connected components, check each component to see if it touches both the left and right boundaries or both the top and bottom boundaries.
- If such a component exists, it implies a barrier that blocks the path from ((0, 0)) to ((xCorner, yCorner)).
-
Return the Result:
- If a blocking barrier is found, return
false.
- Otherwise, return
true, indicating that such a path exists.
Implementation:
def pathExists(xCorner, yCorner, circles):
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
self.left = [False] * n
self.right = [False] * n
self.top = [False] * n
self.bottom = [False] * n
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
fx = self.find(x)
fy = self.find(y)
if fx == fy:
return
self.parent[fy] = fx
self.left[fx] = self.left[fx] or self.left[fy]
self.right[fx] = self.right[fx] or self.right[fy]
self.top[fx] = self.top[fx] or self.top[fy]
self.bottom[fx] = self.bottom[fx] or self.bottom[fy]
n = len(circles)
uf = UnionFind(n)
for i in range(n):
xi, yi, ri = circles[i]
if xi - ri <= 0:
uf.left[i] = True
if xi + ri >= xCorner:
uf.right[i] = True
if yi - ri <= 0:
uf.bottom[i] = True
if yi + ri >= yCorner:
uf.top[i] = True
# Union overlapping circles
for i in range(n):
xi, yi, ri = circles[i]
for j in range(i+1, n):
xj, yj, rj = circles[j]
dx = xi - xj
dy = yi - yj
if (dx*dx + dy*dy) <= (ri + rj) * (ri + rj):
uf.union(i, j)
# Check for any group that touches both left and right or top and bottom
for i in range(n):
fi = uf.find(i)
if uf.left[fi] and uf.right[fi]:
return False
if uf.top[fi] and uf.bottom[fi]:
return False
# Additionally, check if start or end is inside any circle
for xi, yi, ri in circles:
if xi*xi + yi*yi <= ri*ri:
return False
if (xi - xCorner)**2 + (yi - yCorner)**2 <= ri*ri:
return False
return True
Explanation:
-
Union-Find Initialization: We initialize a Union-Find structure where each circle is its own parent. We also keep track of whether each set touches any of the four boundaries.
-
Union Overlapping Circles: For each pair of circles, we check if they overlap. If they do, we union their sets and update the boundary touch information.
-
Check for Blocking Barriers: After processing all circles, we check if any connected component touches both left and right boundaries or both top and bottom boundaries. If such a barrier exists, it's impossible to find a valid path.
-
Edge Cases: Additionally, we ensure that neither the start point ((0, 0)) nor the end point ((xCorner, yCorner)) lies within or on any of the circles.
-
Result: If no blocking barrier exists and both start and end points are not inside any circle, we return True, indicating that a valid path exists.
Complexity Analysis:
- Time Complexity: (O(n^2)), where (n) is the number of circles. This is due to the pairwise checking of circles for overlaps.
- Space Complexity: (O(n)), mainly for the Union-Find structure.
Conclusion:
This approach efficiently determines whether a valid path exists by leveraging the Union-Find data structure to detect potential barriers formed by overlapping circles. If no such barrier exists, the path from ((0, 0)) to ((xCorner, yCorner)) that avoids all circles is possible.