public static class ConsoleHelper
public static void PrintMaze(string label, int[, ] array)
label = label ?? "The maze looks like: ";
StringBuilder result = new StringBuilder();
for (int i = 0; i < array.GetLength(0); i++)
StringBuilder line = new StringBuilder();
for (int j = 0; j < array.GetLength(1); j++)
line.Append($"{array[i, j], 15}");
result.AppendLine(line.ToString());
Console.WriteLine($"{label}");
Console.WriteLine(result);
public static class ArrayUtilities
public static int[, ] WayOutOfMaze(int[, ] maze)
int[, ] stepsMatrix = new int[maze.GetLength(0), maze.GetLength(1)];
for (int i = 0; i < maze.GetLength(0); i++)
for (int j = 0; j < maze.GetLength(1); j++)
bool pacManFound = false;
MatrixItem exit = FindExit(maze);
MatrixItem pacMan = FindPacMan(maze);
stepsMatrix[exit.Row, exit.Column] = currentWeight;
for (int i = 0; i < maze.GetLength(0); i++)
for (int j = 0; j < maze.GetLength(1); j++)
bool isCloseNeighbourTop = (i - 1 >= 0)
&& stepsMatrix[i - 1, j] == currentWeight
&& stepsMatrix[i, j] == -1;
stepsMatrix[i, j] = currentWeight + 1;
bool isCloseNeighbourLeft = (j - 1 >= 0)
&& stepsMatrix[i, j - 1] == currentWeight
&& stepsMatrix[i, j] == -1;
if (isCloseNeighbourLeft)
stepsMatrix[i, j] = currentWeight + 1;
bool isCloseNeighbourBottom = (i + 1 < maze.GetLength(0))
&& stepsMatrix[i + 1, j] == currentWeight
&& stepsMatrix[i, j] == -1;
if (isCloseNeighbourBottom)
stepsMatrix[i, j] = currentWeight + 1;
bool isCloseNeighbourRight = (j + 1 < maze.GetLength(1))
&& stepsMatrix[i, j + 1] == currentWeight
&& stepsMatrix[i, j] == -1;
if (isCloseNeighbourRight)
stepsMatrix[i, j] = currentWeight + 1;
if (stepsMatrix[pacMan.Row, pacMan.Column] >= 0)
public static MatrixItem FindExit(int[, ] maze)
MatrixItem exit = new MatrixItem();
for (int row = 0; row < maze.GetLength(0); row++)
for (int col = 0; col < maze.GetLength(1); col++)
if (maze[row, col] == -int.MaxValue)
public static MatrixItem FindPacMan(int[, ] maze)
MatrixItem pacMan = new MatrixItem();
for (int row = 0; row < maze.GetLength(0); row++)
for (int col = 0; col < maze.GetLength(1); col++)
if (maze[row, col] == int.MaxValue)
public static string PrintWayOutOfMaze(string label, int[, ] stepMatrix)
label = label ?? "The way out is: ";
StringBuilder wayOut = new StringBuilder();
for (int row = 0; row < stepMatrix.GetLength(0); row++)
for (int col = 0; col < stepMatrix.GetLength(1); col++)
if (stepMatrix[row, col] > maxSteps)
maxSteps = stepMatrix[row, col];
wayOut.AppendLine($"Maximum no of steps is: {maxSteps}");
wayOut.Append($"{label}");
for (int row = 0; row < stepMatrix.GetLength(0); row++)
for (int col = 0; col < stepMatrix.GetLength(1); col++)
if (stepMatrix[row, col] == maxSteps)
wayOut.Append($"exit: ");
wayOut.Append($"({row},{col})");
return wayOut.ToString();
public static void Main()
{ -1, 0, 0, 0, int.MaxValue},
{ -1, -1, -1, 0, -int.MaxValue}
ConsoleHelper.PrintMaze("The Pac-Man maze looks like: ", maze);
int[,] wayOutMaze = ArrayUtilities.WayOutOfMaze(maze);
ConsoleHelper.PrintMaze("The step matrix looks like: ", wayOutMaze);
string result = ArrayUtilities.PrintWayOutOfMaze("The way out is: ", wayOutMaze);
Console.WriteLine(result);