Detailed Solution for beginners 🤗🤗🤗🤗
Welcome, I hope you will have a wonderfull day \uD83E\uDD17\uD83E\uDD17\uD83E\uDD17\uD83E\uDD17\n\nNow, on to the Solution \u23ED\uFE0F\n# Approach\n\n1. Define
# Welcome, I hope you will have a wonderfull day \uD83E\uDD17\uD83E\uDD17\uD83E\uDD17\uD83E\uDD17\n\nNow, on to the Solution \u23ED\uFE0F\n# Approach\n\n1. **Define a Helper Class for Coordinates**\nWe begin by defining a helper class Cord to store the coordinates (`x` and `y`), as well as the distance from the entrance (`distFromEntrance`). This helps in easily managing queue operations for the BFS algorithm.\n\n    ```\n    class Cord {\n        int x;\n        int y;\n        int distFromEntrance;\n\n        public Cord(int x, int y, int distFromEntrance) {\n            this.x = x;\n            this.y = y;\n            this.distFromEntrance = distFromEntrance;\n        }\n    }\n    ```\n\n\n2. **Initialize BFS Components**\nWe set up the BFS using a queue to explore the maze, starting from the entrance. We also mark the entrance to prevent revisiting.\n\n\n    ```\n    Queue<Cord> pathToExit = new ArrayDeque<>();\n    // Start BFS from the entrance\n    pathToExit.add(new Cord(entrance[0], entrance[1], 0)); \n    ```\n\n3. **Explore the Maze Using BFS**\nWe use BFS to explore each possible path \uD83D\uDEE3\uFE0F from the entrance until an exit is found. We check all four possible directions (up, down, left, right) at each step. If the current cell is an exit (not \u274C the entrance), the shortest path length is returned.\n\n```\nwhile (!pathToExit.isEmpty()) {\n    \n    // Dequeue the current position for exploration\n    Cord curr = pathToExit.poll(); \n    boolean isExit = (curr.x == 0 || curr.x == rows - 1 || curr.y == 0 || curr.y == cols - 1)\n && !(curr.x == entrance[0] && curr.y == entrance[1]);\n\n    // Check if current position is an exit\n    if (isExit) return curr.distFromEntrance; \n\n    for (int[] dir : directions) {\n        int newX = curr.x + dir[0];\n        int newY = curr.y + dir[1];\n\n        boolean isOutside = newX < 0 || newX >= rows || newY < 0 || newY >= cols;\n\n        // Skip if it\'s outside the maze bounds or a wall\n        if (isOutside || maze[newX][newY] == \'+\') continue; \n\n\n        // Mark the cell as visited by setting it as a wall\n        maze[newX][newY] = \'+\'; \n        pathToExit.add(new Cord(newX, newY, curr.distFromEntrance + 1));\n    }\n}\n```\n\n\n\n# Complexity\n- **Time complexity:**\n$$O(n * m)$$ - The time complexity of this solution is `O(n * m)`, where `n` is the number of rows and `m` is the number of columns in the `maze`. This complexity arises from the breadth-first search (BFS) approach used in the solution, which in the worst case \uD83E\uDD28 might explore every cell in the maze once.\n\n- **Space complexity:**\n$$O(n * m)$$ - In the worst-case \uD83E\uDD28 scenario, the queue could theoretically hold a significant portion of the maze\'s cells at the same time, especially if many cells are reachable and processed at roughly the same depth level in the BFS.\n\n# Code\n```\nclass Solution {\n    class Cord {\n        int x;\n        int y;\n        int distFromEntrance;\n\n        public Cord(int x, int y, int distFromEntrance) {\n            this.x = x;\n            this.y = y;\n            this.distFromEntrance = distFromEntrance;\n        }\n    }\n\n    public int nearestExit(char[][] maze, int[] entrance) {\n        Queue<Cord> pathToExit = new ArrayDeque<>();\n        int rows = maze.length;\n        int cols = maze[0].length;\n\n        pathToExit.add(new Cord(entrance[0], entrance[1], 0));\n        int[][] directions = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };\n\n        while (!pathToExit.isEmpty()) {\n            Cord curr = pathToExit.poll();\n            boolean isExit = curr.x == 0 || curr.x == rows - 1 || curr.y == 0 || curr.y == cols - 1;\n            boolean isEntrance = curr.x == entrance[0] && curr.y == entrance[1];\n\n            if (isExit && !isEntrance)\n                return curr.distFromEntrance;\n\n            for (int[] dir : directions) {\n                int newX = curr.x + dir[0];\n                int newY = curr.y + dir[1];\n\n                boolean isOutside = newX < 0 || newX >= rows || newY < 0 || newY >= cols;\n                if (isOutside || maze[newX][newY] == \'+\')\n                    continue;\n\n                maze[newX][newY] = \'+\';\n                pathToExit.add(new Cord(newX, newY, curr.distFromEntrance + 1));\n            }\n        }\n        return -1;\n    }\n}\n```
['Breadth-First Search', 'Graph', 'Java']