<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:
        N = len(circles)
        adj = defaultdict(set)
        # N = top
        # N + 1 = bottom
        for i, (x, y, r) in enumerate(circles):
            if (y - Y) ** 2 + (x - X) ** 2 <= r ** 2 or y ** 2 + x ** 2 <= r ** 2:
                return False
            if y - r >= Y or y + r <= 0 or x - r >= X or x + r <= 0:
                continue
            if (y + r >= Y and 0 <= x <= X) or (x - r <= 0 and 0 <= y <= Y) or (x <= 0 and y >= Y):
                adj[N].add(i)
                adj[i].add(N)
            if (y - r <= 0 and 0 <= x <= X) or (x + r >= X and 0 <= y <= Y) or (x >= X and y <= 0):
                adj[N+1].add(i)
                adj[i].add(N+1)

        for i in range(N-1):
            for j in range(i+1, N):
                if (circles[i][0] - circles[j][0]) ** 2 + (circles[i][1] - circles[j][1]) ** 2 <= (circles[i][2] + circles[j][2]) ** 2:
                    if (((circles[i][0] + circles[j][0]) // 2) >= X and ((circles[i][1] + circles[j][1]) // 2) >= Y) or (((circles[i][1] + circles[j][1]) // 2) <= 0 and ((circles[i][0] + circles[j][0]) // 2) <= 0):
                        continue
                    adj[i].add(j)
                    adj[j].add(i)


        def go(start, target):
            visit = set()
            def dfs(node):
                if node == target:
                    return True
                if node in visit:
                    return False
                visit.add(node)
                for nei in adj[node]:
                    if dfs(nei):
                        return True
                
                return False
            return dfs(start)
        
        return not(go(N, N+1))

        
63171