<p>You are given two positive integers <code>xCorner</code> and <code>yCorner</code>, and a 2D array <code>circles</code>, where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> denotes a circle with center at <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code>.</p>

<p>There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate <code>(xCorner, yCorner)</code>. You need to check whether there is a path from the bottom left corner to the top right corner such that the <strong>entire path</strong> lies inside the rectangle, <strong>does not</strong> touch or lie inside <strong>any</strong> circle, and touches the rectangle <strong>only</strong> at the two corners.</p>

<p>Return <code>true</code> if such a path exists, and <code>false</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 4, circles = [[2,1,1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">true</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/18/example2circle1.png" style="width: 346px; height: 264px;" /></p>

<p>The black curve shows a possible path between <code>(0, 0)</code> and <code>(3, 4)</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[1,1,2]]</span></p>

<p><strong>Output:</strong> <span class="example-io">false</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/18/example1circle.png" style="width: 346px; height: 264px;" /></p>

<p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">false</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/18/example0circle.png" style="width: 346px; height: 264px;" /></p>

<p>No path exists from <code>(0, 0)</code> to <code>(3, 3)</code>.</p>
</div>

<p><strong class="example">Example 4:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">xCorner = 4, yCorner = 4, circles = [[5,5,1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">true</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/04/rectangles.png" style="width: 346px; height: 264px;" /></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= xCorner, yCorner &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= circles.length &lt;= 1000</code></li>
	<li><code>circles[i].length == 3</code></li>
	<li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def canReachCorner(self, X: int, Y: int, circles: List[List[int]]) -> bool:
        # merge when the circle matches
        n = len(circles)
        adj_list = defaultdict(list)

        for i in range(n - 1):
            for j in range(i + 1, n):
                x1, y1, r1 = circles[i]
                x2, y2, r2 = circles[j]

                if ((x2 - x1)**2 + (y2 - y1)**2)**0.5 <= r1 + r2:
                    adj_list[i].append(j)
                    adj_list[j].append(i)
        
        UPPER = n
        RIGHT = n + 1
        LOWER = n + 2
        LEFT = n + 3
        for i in range(n):
            x, y, r = circles[i]

            ry_temp = r**2 - (X - x)**2
            if ry_temp >= 0:
                ry1, ry2 = -(ry_temp**0.5) + y, ry_temp**0.5 + y
                if ry1 <= Y and 0 <= ry2:
                    adj_list[RIGHT].append(i)
                    adj_list[i].append(RIGHT)

            ly_temp = r**2 - x**2
            if ly_temp >= 0:
                ly1, ly2 = -(ly_temp**0.5) + y, ly_temp**0.5 + y
                if ly1 <= Y and 0 <= ly2:
                    adj_list[LEFT].append(i)
                    adj_list[i].append(LEFT)

            ux_temp = r**2 - (Y - y)**2
            if ux_temp >= 0:
                ux1, ux2 = -(ux_temp**0.5) + x, ux_temp**0.5 + x
                if ux1 <= X and 0 <= ux2:
                    adj_list[UPPER].append(i)
                    adj_list[i].append(UPPER)

            lx_temp = r**2 - y**2
            if lx_temp >= 0:
                lx1, lx2 = -(lx_temp**0.5) + x, lx_temp**0.5 + x
                if lx1 <= X and 0 <= lx2:
                    adj_list[LOWER].append(i)
                    adj_list[i].append(LOWER)

        queue = deque([UPPER])
        visit = [0] * (n + 4)
        while queue:
            v = queue.popleft()
            for n_v in adj_list[v]:
                if n_v in [LOWER, RIGHT]:
                    return False
                if visit[n_v]:
                    continue
                visit[n_v] = 1
                queue.append(n_v)

        queue = deque([LEFT])
        visit = [0] * (n + 4)
        while queue:
            v = queue.popleft()
            for n_v in adj_list[v]:
                if n_v in [LOWER, RIGHT]:
                    return False
                if visit[n_v]:
                    continue
                visit[n_v] = 1
                queue.append(n_v)

        return True

        
38703