using
System;
using
System.Collections.Generic;
class
GFG{
static
readonly
int
ROW = 3;
static
readonly
int
COL = 3;
static
int
[]dir_Row = { -1, 0, 1, 0 };
static
int
[]dir_Col = { 0, 1, 0, -1 };
class
pointLoc
{
public
int
x, y, k;
public
pointLoc(
int
x,
int
y,
int
k)
{
this
.x = x;
this
.y = y;
this
.k = k;
}
};
class
pair
{
public
int
first, second;
public
pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
static
int
BFS(
int
[,]matrix,
int
k, pair source,
pair destination)
{
Queue<pointLoc> q =
new
Queue<GFG.pointLoc>();
int
[,] distance =
new
int
[ROW, COL];
int
[,] obstacles =
new
int
[ROW, COL];
q.Enqueue(
new
pointLoc(source.first,
source.second, k));
while
(q.Count != 0)
{
pointLoc te = q.Peek();
int
x = te.x;
int
y = te.y;
int
tk = te.k;
if
(x == destination.first &&
y == destination.second)
return
distance[x, y];
q.Dequeue();
if
(matrix[x, y] == 1)
{
if
(tk > 0)
tk--;
else
continue
;
}
if
(obstacles[x, y] >= tk)
continue
;
obstacles[x, y] = tk;
for
(
int
i = 0; i < 4; i++)
{
int
ax = x + dir_Row[i];
int
ay = y + dir_Col[i];
if
(ax < 0 || ay < 0 ||
ax >= ROW || ay >= COL)
continue
;
q.Enqueue(
new
pointLoc(ax, ay, tk));
distance[ax, ay] = distance[x, y] + 1;
}
}
return
-1;
}
public
static
void
Main(String[] args)
{
int
[,]matrix = { { 0, 0, 1 },
{ 1, 0, 1 },
{ 0, 1, 0 } };
int
k = 2;
pair source =
new
pair(0, 0);
pair destination =
new
pair(2, 2);
Console.Write(BFS(matrix, k, source, destination));
}
}