C# BFS Solution
Intuition\n Describe your first thoughts on how to solve this problem. To solve the problem of finding the shortest path to the nearest exit in the maze, we can
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->To solve the problem of finding the shortest path to the nearest exit in the maze, we can approach it as a breadth-first search (BFS) problem. BFS is ideal for exploring all possible paths from the entrance, layer by layer, to ensure that the first exit we reach is the shortest path.\n____\n\n# Approach\n<!-- Describe your approach to solving the problem. -->Initialize the BFS:\n\nUse a queue to track the cells we need to visit, starting from the entrance.\nTrack the number of steps taken to reach each cell.\nMark the entrance as visited to prevent revisiting it.\nBFS Algorithm:\n\nFor each cell in the queue, check its neighboring cells (up, down, left, right).\nIf a neighboring cell is an exit (border cell) and not the entrance, return the number of steps.\nIf a neighboring cell is an empty cell and not visited, add it to the queue and mark it as visited.\nEdge Cases:\n\nIf there are no exits or if all paths are blocked, return -1.\n___\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(m * n), where \'m\' is the number of rows in the maze and \'n\' is the number of columns in the maze.\n____\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->O(m * n).\n___\n\n# Code\n```csharp []\nusing System.Collections.Generic;\n\npublic class Solution {\n    public int NearestExit(char[][] maze, int[] entrance) {\n        int m = maze.Length;\n        int n = maze[0].Length;\n        \n        // Directions for moving up, down, left, right\n        int[][] directions = new int[][] {\n            new int[] {-1, 0},  // Up\n            new int[] {1, 0},   // Down\n            new int[] {0, -1},  // Left\n            new int[] {0, 1}    // Right\n        };\n        \n        Queue<(int, int, int)> queue = new Queue<(int, int, int)>();\n        queue.Enqueue((entrance[0], entrance[1], 0));  // Add entrance with 0 steps\n        maze[entrance[0]][entrance[1]] = \'+\';  // Mark the entrance as visited\n        \n        // BFS loop\n        while (queue.Count > 0) {\n            var (x, y, steps) = queue.Dequeue();\n            \n            foreach (var dir in directions) {\n                int newX = x + dir[0];\n                int newY = y + dir[1];\n                \n                // Check if new position is within bounds and is an empty cell\n                if (newX >= 0 && newX < m && newY >= 0 && newY < n && maze[newX][newY] == \'.\') {\n                    // Check if it\'s a border cell (exit)\n                    if (newX == 0 || newX == m - 1 || newY == 0 || newY == n - 1) {\n                        return steps + 1;\n                    }\n                    \n                    // Mark as visited and add to the queue\n                    maze[newX][newY] = \'+\';\n                    queue.Enqueue((newX, newY, steps + 1));\n                }\n            }\n        }\n        \n        // If no exit is found\n        return -1;\n    }\n}\n```
['Array', 'Breadth-First Search', 'Matrix', 'C#']