using
System;
using
System.Collections.Generic;
public
class
GFG
{
static
int
[] dxi = { 1, 0, -1, 0, -1, 1 };
static
int
[] dyj = { 0, 1, 0, -1, -1, 1 };
static
bool
IsValid(
int
[,] grid,
int
x,
int
y,
int
m,
int
n)
{
return
x >= 0 && x < m && y >= 0 && y < n
&& grid[x, y] == 1;
}
static
bool
IsDestinationReachable(
int
[,] grid,
int
sx,
int
sy,
int
dx,
int
dy,
int
k)
{
int
m = grid.GetLength(0);
int
n = grid.GetLength(1);
Queue<
int
[]> Queue =
new
Queue<
int
[]>();
int
[,] distances =
new
int
[m, n];
for
(
int
i = 0; i < m; i++)
{
for
(
int
j = 0; j < n; j++)
{
distances[i, j] = -1;
}
}
Queue.Enqueue(
new
int
[] { sx, sy });
distances[sx, sy] = 0;
while
(Queue.Count > 0)
{
int
[] p = Queue.Dequeue();
int
x = p[0];
int
y = p[1];
if
(x == dx && y == dy)
{
return
true
;
}
for
(
int
i = 0; i < 4; i++)
{
int
new_x = x + dxi[i];
int
new_y = y + dyj[i];
if
(IsValid(grid, new_x, new_y, m, n)
&& distances[new_x, new_y] == -1)
{
Queue.Enqueue(
new
int
[] { new_x, new_y });
distances[new_x, new_y]
= distances[x, y] + 1;
if
(distances[new_x, new_y] > k)
{
return
false
;
}
}
}
}
return
false
;
}
public
static
void
Main (
string
[] args) {
int
[,] grid =
new
int
[,] { { 1, 0, 1, 1 },
{ 1, 0, 1, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 } };
int
sx = 0;
int
sy = 0;
int
dx = 3;
int
dy = 3;
int
k = 3;
if
(IsDestinationReachable(grid, sx, sy, dx, dy,
k)) {
Console.WriteLine(
"True"
);
}
else
{
Console.WriteLine(
"False"
);
}
}
}