private static int MarkPossibleDirections(MatrixPosition matrixPosition)
        {
            int rowsLength = maze.GetLength(0),
                colsLength = maze.GetLength(1);

            MatrixPosition[] allPositions =
            {
                new MatrixPosition(matrixPosition.Row - 1, matrixPosition.Col),
                new MatrixPosition(matrixPosition.Row,     matrixPosition.Col + 1),
                new MatrixPosition(matrixPosition.Row + 1, matrixPosition.Col),
                new MatrixPosition(matrixPosition.Row,     matrixPosition.Col - 1)
            };

            int unexploredCount = 0;

            foreach (var position in allPositions)
            {
                if (position.IsWithinRange(rowsLength, colsLength))
                {
                    if (maze[position.Row, position.Col].Equals("0", StringComparison.Ordinal))
                    {
                        matrixPosition.Unexplored.StackUp(position);
                        unexploredCount++;
                    }
                }
            }

            return(unexploredCount);
        }
        private static void DoNextLabyrinth()
        {
            MatrixPosition start = GetStartingPosition(maze, "*");

            start.DistanceFromStartPosition = 0;
            MarkPossibleDirections(start);

            while (start.Unexplored.Size > 0)
            {
                var next = start.Next;
                next.DistanceFromStartPosition = 1;

                unexploredPositions.Enqueue(next);
            }

            ExploreMaze(unexploredPositions.GetNextInLine());

            MarkUnreachedPlaces(maze, "0", "u");
        }
        private static void ExploreMaze(MatrixPosition position)
        {
            maze[position.Row, position.Col] = position.DistanceFromStartPosition.ToString();

            MarkPossibleDirections(position);

            while (position.Unexplored.Size > 0)
            {
                var next = position.Next;
                next.DistanceFromStartPosition = position.DistanceFromStartPosition + 1;

                unexploredPositions.Enqueue(next);
            }

            if (unexploredPositions.Size > 0)
            {
                ExploreMaze(unexploredPositions.GetNextInLine());
            }
        }
        private static int MarkPossibleDirections(MatrixPosition matrixPosition)
        {
            int rowsLength = maze.GetLength(0),
                colsLength = maze.GetLength(1);

            MatrixPosition[] allPositions =
            {
                new MatrixPosition(matrixPosition.Row - 1, matrixPosition.Col),
                new MatrixPosition(matrixPosition.Row, matrixPosition.Col + 1),
                new MatrixPosition(matrixPosition.Row + 1, matrixPosition.Col),
                new MatrixPosition(matrixPosition.Row, matrixPosition.Col - 1)
            };

            int unexploredCount = 0;
            foreach (var position in allPositions)
            {
                if (position.IsWithinRange(rowsLength, colsLength))
                {
                    if (maze[position.Row, position.Col].Equals("0", StringComparison.Ordinal))
                    {
                        matrixPosition.Unexplored.StackUp(position);
                        unexploredCount++;
                    }
                }
            }

            return unexploredCount;
        }
        private static void ExploreMaze(MatrixPosition position)
        {
            maze[position.Row, position.Col] = position.DistanceFromStartPosition.ToString();

            MarkPossibleDirections(position);

            while (position.Unexplored.Size > 0)
            {
                var next = position.Next;
                next.DistanceFromStartPosition = position.DistanceFromStartPosition + 1;

                unexploredPositions.Enqueue(next);
            }

            if (unexploredPositions.Size > 0)
            {
                ExploreMaze(unexploredPositions.GetNextInLine());
            }
        }