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
publicclassSolution {int[] x = { 1,1,1,-1,-1,-1,0,0 };int[] y = { -1,1,0,-1,1,0,1,-1 };publicbooleanisValid(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)returntrue;returnfalse; }publicStringsolve(int A,int B,int n,int r,int[] xCoord,int[] yCoord) {int[][] valid =newint[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 =newboolean[A +1][B +1]; visited[0][0] =true;Queue<Integer> q =newLinkedList<Integer>();q.add(0);// Adding starting x coordq.add(0);// Adding starting y coordwhile (!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"; }}