using
System;
using
System.Collections.Generic;
class
GFG {
static
string
Solvable(
int
[, ] mat,
Dictionary<
string
,
int
> Given_keys,
int
xDestination,
int
yDestination)
{
HashSet<
int
> ListOfAvailableKeys =
new
HashSet<
int
>();
ListOfAvailableKeys.Add(1);
List<
int
[]> q =
new
List<
int
[]>();
q.Add(
new
[] { 0, 0 });
int
[, ] dirr =
new
int
[, ] { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
Dictionary<
int
, List<
int
[]> > Unopened_Rooms =
new
Dictionary<
int
, List<
int
[]> >();
while
(q.Count > 0) {
int
[] curr = q[0];
q.RemoveAt(0);
if
(curr[0] == xDestination
&& curr[1] == yDestination) {
return
"True"
;
}
string
key = Convert.ToString(curr[0]) +
"#"
+ Convert.ToString(curr[1]);
if
(Given_keys.ContainsKey(key)) {
int
curr_key = Given_keys[key];
ListOfAvailableKeys.Add(curr_key);
if
(Unopened_Rooms.ContainsKey(curr_key)) {
for
(
var
i = 0;
i < Unopened_Rooms[curr_key].Count;
i++) {
q.Add(
new
[] {
Unopened_Rooms[curr_key][i][0],
Unopened_Rooms[curr_key][i][1] });
}
}
}
for
(
int
i = 0; i < dirr.GetLength(0); i++) {
int
x = curr[0] + dirr[i, 0];
int
y = curr[1] + dirr[i, 1];
if
(x < 0 || y < 0 || x >= mat.GetLength(0)
|| y >= mat.GetLength(1) || mat[x, y] == 0) {
continue
;
}
if
(!ListOfAvailableKeys.Contains(mat[x, y])) {
if
(!Unopened_Rooms.ContainsKey(
mat[x, y]))
Unopened_Rooms[(mat[x, y])] =
new
List<
int
[]>();
Unopened_Rooms[(mat[x, y])].Add(
new
[] { x, y });
mat[x, y] = 0;
}
else
{
q.Add(
new
[] { x, y });
mat[x, y] = 0;
}
}
}
return
"False"
;
}
public
static
void
Main(
string
[] args)
{
int
[, ] mat =
new
[, ] { { 1, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 2, 0, 0 }, { 1, 1, 2, 1 } };
Dictionary<
string
,
int
> keys =
new
Dictionary<
string
,
int
>();
keys[
"1#1"
] = 3;
keys[
"1#3"
] = 2;
int
x = 3;
int
y = 3;
Console.WriteLine(Solvable(mat, keys, x, y));
}
}