using
System;
class
GFG {
static
bool
isSafe(
int
x,
int
y,
bool
[,] Visited)
{
return
(x >= 0 && x < 4 && y >= 0 &&
y < 3 && !Visited[x,y]);
}
static
void
DFS(
bool
[,] visited,
int
[,] Keypad,
int
n,
string
pattern,
int
x,
int
y)
{
pattern = pattern + (Keypad[x,y]).ToString();
if
(pattern.Length == n) {
Console.Write(pattern +
" "
);
return
;
}
int
[] row = { 0, 1, 0, -1 };
int
[] col = { 1, 0, -1, 0 };
visited[x,y] =
true
;
for
(
int
k = 0; k < 4; k++)
if
(isSafe(x + row[k], y + col[k], visited)
&& Keypad[x + row[k],y + col[k]] != -1)
DFS(visited, Keypad, n, pattern,
x + row[k], y + col[k]);
visited[x,y] =
false
;
pattern = pattern.Substring(0, pattern.Length - 1);
}
static
void
patternOfSizeK(
int
[,] Keypad,
int
n)
{
bool
[,] visited =
new
bool
[4,3];
for
(
int
i = 0; i < 4; i++)
{
for
(
int
j = 0; j < 3; j++)
{
visited[i,j] =
false
;
}
}
for
(
int
i = 0; i < 4; i++)
{
for
(
int
j = 0; j < 3; j++)
{
if
(Keypad[i,j] != -1)
DFS(visited, Keypad, n,
""
, i, j);
}
}
}
static
void
Main() {
int
[,] Keypad = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ -1, 0, -1 } };
int
n = 3;
patternOfSizeK(Keypad, n);
}
}