<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>

from math import sqrt

class Solution:
    def canReachCorner(self, X: int, Y: int, circles: List[List[int]]) -> bool:
        n_circles = len(circles)
        graph = [set() for _ in range(n_circles + 4)]
        istouch = lambda coor, r, b: coor - r <= b <= coor + r
        for idx, circle in enumerate(circles):
            cx, cy, cr = circle
            if istouch(cy, cr, Y): # up bound
                graph[idx].add(n_circles)
                graph[n_circles].add(idx)
            if istouch(cy, cr, 0): # low bound
                graph[idx].add(n_circles + 1)
                graph[n_circles + 1].add(idx)
            if istouch(cx, cr, 0): # left bound
                graph[idx].add(n_circles + 2)
                graph[n_circles + 2].add(idx)
            if istouch(cx, cr, X): # right bound
                graph[idx].add(n_circles + 3)
                graph[n_circles + 3].add(idx)
        dist = lambda x1, y1, x2, y2: sqrt((y2 - y1) ** 2 + (x2 - x1) ** 2)
        isctouch = lambda c1x, c1y, c1r, c2x, c2y, c2r: dist(c1x, c1y, c2x, c2y) <= c1r + c2r
        for i in range(n_circles):
            for j in range(i):
                cix, ciy, cir = circles[i]
                cjx, cjy, cjr = circles[j]
                if isctouch(cix, ciy, cir, cjx, cjy, cjr):
                    graph[i].add(j)
                    graph[j].add(i)
        cur_set = {n_circles, n_circles + 2}
        visited = set()
        tar_set = {n_circles + 1, n_circles + 3}
        while len(cur_set) > 0:
            nxt_set = set()
            for c_node in cur_set:
                nxt_set |= graph[c_node]
            if len(tar_set & nxt_set) > 0:
                return False
            visited |= cur_set
            nxt_set -= visited
            cur_set = nxt_set
        return True
56828