Check if the Rectangle Corner Is Reachable Problem
Description
LeetCode Problem 3235.
You are given two positive integers xCorner and yCorner, and a 2D array circles, where circles[i] = [x_i, y_i, r_i] denotes a circle with center at (x_i, y_i) and radius r_i.
There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate (xCorner, yCorner). You need to check whether there is a path from the bottom left corner to the top right corner such that the entire path lies inside the rectangle, does not touch or lie inside any circle, and touches the rectangle only at the two corners.
Return true if such a path exists, and false otherwise.
Example 1:
Input: xCorner = 3, yCorner = 4, circles = [[2,1,1]]
Output: true
Explanation:

The black curve shows a possible path between (0, 0) and (3, 4).
Example 2:
Input: xCorner = 3, yCorner = 3, circles = [[1,1,2]]
Output: false
Explanation:

No path exists from (0, 0) to (3, 3).
Example 3:
Input: xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]
Output: false
Explanation:

No path exists from (0, 0) to (3, 3).
Example 4:
Input: xCorner = 4, yCorner = 4, circles = [[5,5,1]]
Output: true
Explanation:

Constraints:
3 <= xCorner, yCorner <= 10^91 <= circles.length <= 1000circles[i].length == 31 <= x_i, y_i, r_i <= 10^9
Solution
Approach: DFS + Geometry
The corner (xCorner, yCorner) is unreachable if either corner is inside a circle, or if a chain of overlapping circles joins the left/top sides of the rectangle to the bottom/right sides, since that chain walls off the path. So return false if (0, 0) or the target lies in any circle. Otherwise, start a DFS from every circle that touches the left or top side, and walk to circles that overlap the current one at a point inside the rectangle. If the DFS reaches a circle touching the bottom or right side, the path is blocked and the answer is false.
Two circles overlap when the squared distance of their centres is at most (r1 + r2)^2. The point on the segment between the centres that divides it in the ratio r1 : r2 lies in both circles whenever they overlap, and it is ((x1*r2 + x2*r1) / (r1 + r2), (y1*r2 + y2*r1) / (r1 + r2)); the circles count as joined inside the rectangle when this point is strictly inside it. Use exact integer arithmetic (64-bit or big integers) for all of this.
Complexity: O(n^2) time, O(n) space, where n is the number of circles.
class Solution:
def canReachCorner(
self, xCorner: int, yCorner: int, circles: List[List[int]]
) -> bool:
def in_circle(x: int, y: int, cx: int, cy: int, r: int) -> int:
return (x - cx) ** 2 + (y - cy) ** 2 <= r**2
def cross_left_top(cx: int, cy: int, r: int) -> bool:
a = abs(cx) <= r and 0 <= cy <= yCorner
b = abs(cy - yCorner) <= r and 0 <= cx <= xCorner
return a or b
def cross_right_bottom(cx: int, cy: int, r: int) -> bool:
a = abs(cx - xCorner) <= r and 0 <= cy <= yCorner
b = abs(cy) <= r and 0 <= cx <= xCorner
return a or b
def dfs(i: int) -> bool:
x1, y1, r1 = circles[i]
if cross_right_bottom(x1, y1, r1):
return True
vis[i] = True
for j, (x2, y2, r2) in enumerate(circles):
if vis[j] or not ((x1 - x2) ** 2 + (y1 - y2) ** 2 <= (r1 + r2) ** 2):
continue
if (
(x1 * r2 + x2 * r1 < (r1 + r2) * xCorner)
and (y1 * r2 + y2 * r1 < (r1 + r2) * yCorner)
and dfs(j)
):
return True
return False
vis = [False] * len(circles)
for i, (x, y, r) in enumerate(circles):
if in_circle(0, 0, x, y, r) or in_circle(xCorner, yCorner, x, y, r):
return False
if (not vis[i]) and cross_left_top(x, y, r) and dfs(i):
return False
return True