> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/graphs-bfs-and-dfs/valid-path.md).

# Valid Path

There is a rectangle with left bottom as `(0, 0)` and right up as `(x, y)`. There are `N circles` such that their centers are inside the `rectangle`.\
Radius of each circle is `R`. Now we need to find out if it is possible that we can move from `(0, 0)` to `(x, y)` without touching any `circle.`

**Note :** We can move from any cell to any of its `8 adjecent neighbours` and we cannot move outside the boundary of the rectangle at any point of time.\
\
\
**Input Format**

```
1st argument given is an Integer x.
2nd argument given is an Integer y.
3rd argument given is an Integer N, number of circles.
4th argument given is an Integer R, radius of each circle.
5th argument given is an Array A of size N, where A[i] = x cordinate of ith circle
6th argument given is an Array B of size N, where B[i] = y cordinate of ith circle
```

**Output Format**

```
Return YES or NO depending on weather it is possible to reach cell (x,y) or not starting from (0,0).
```

**Constraints**

```
0 <= x, y, R <= 100
1 <= N <= 1000
Center of each circle would lie within the grid
```

**For Example**

```
Input:
    x = 2
    y = 3
    N = 1
    R = 1
    A = [2]
    B = [3]
Output:
    NO
   
Explanation:
    There is NO valid path in this case
```

```java
public class Solution {
    int[] x = { 1, 1, 1, -1, -1, -1, 0, 0 };
    int[] y = { -1, 1, 0, -1, 1, 0, 1, -1 };

    public boolean isValid(int[][] valid, boolean[][] visited, int x, int y, int A, int B) {
        if (!(x < 0 || y < 0 || x >= A + 1 || y >= B + 1) && !visited[x][y] && valid[x][y] != -1)
            return true;
        return false;
    }

    public String solve(int A, int B, int n, int r, int[] xCoord, int[] yCoord) {
        int[][] valid = new int[A + 1][B + 1];
        for (int i = 0; i <= A; i++) {
            for (int j = 0; j <= B; j++) {
                for (int z = 0; z < n; z++) {
                    if (Math.sqrt(Math.pow(xCoord[z] - i, 2) + Math.pow(yCoord[z] - j, 2)) <= r)
                        valid[i][j] = -1; // Marking all the points within radius of any circle as -1
                }
            }
        }
        if (valid[0][0] == -1 || valid[A][B] == -1)
            return "NO";
        boolean[][] visited = new boolean[A + 1][B + 1];
        visited[0][0] = true;
        Queue<Integer> q = new LinkedList<Integer>();
        q.add(0);// Adding starting x coord
        q.add(0);// Adding starting y coord
        while (!q.isEmpty()) {
            int i = q.poll();
            int j = q.poll();
            for (int z = 0; z < 8; z++) {
                int ix = i + x[z];
                int iy = j + y[z];
                if (isValid(valid, visited, ix, iy, A, B)) {
                    if (ix == A && iy == B)
                        return "YES";
                    q.add(ix);
                    q.add(iy);
                    visited[ix][iy] = true;
                }
            }
        }
        return "NO";
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/graphs-bfs-and-dfs/valid-path.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
