{"id":9285,"date":"2021-12-30T03:07:41","date_gmt":"2021-12-30T11:07:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9285"},"modified":"2021-12-30T03:10:19","modified_gmt":"2021-12-30T11:10:19","slug":"leetcode-1926-nearest-exit-from-entrance-in-maze","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1926-nearest-exit-from-entrance-in-maze\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1926. Nearest Exit from Entrance in Maze"},"content":{"rendered":"\n<p>You are given an&nbsp;<code>m x n<\/code>&nbsp;matrix&nbsp;<code>maze<\/code>&nbsp;(<strong>0-indexed<\/strong>) with empty cells (represented as&nbsp;<code>'.'<\/code>) and walls (represented as&nbsp;<code>'+'<\/code>). You are also given the&nbsp;<code>entrance<\/code>&nbsp;of the maze, where&nbsp;<code>entrance = [entrance<sub>row<\/sub>, entrance<sub>col<\/sub>]<\/code>&nbsp;denotes the row and column of the cell you are initially standing at.<\/p>\n\n\n\n<p>In one step, you can move one cell&nbsp;<strong>up<\/strong>,&nbsp;<strong>down<\/strong>,&nbsp;<strong>left<\/strong>, or&nbsp;<strong>right<\/strong>. You cannot step into a cell with a wall, and you cannot step outside the maze. Your goal is to find the&nbsp;<strong>nearest exit<\/strong>&nbsp;from the&nbsp;<code>entrance<\/code>. An&nbsp;<strong>exit<\/strong>&nbsp;is defined as an&nbsp;<strong>empty cell<\/strong>&nbsp;that is at the&nbsp;<strong>border<\/strong>&nbsp;of the&nbsp;<code>maze<\/code>. The&nbsp;<code>entrance<\/code>&nbsp;<strong>does not count<\/strong>&nbsp;as an exit.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number of steps<\/strong>&nbsp;in the shortest path from the&nbsp;<\/em><code>entrance<\/code><em>&nbsp;to the nearest exit, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if no such path exists<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/06\/04\/nearest1-grid.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> maze = [[\"+\",\"+\",\".\",\"+\"],[\".\",\".\",\".\",\"+\"],[\"+\",\"+\",\"+\",\".\"]], entrance = [1,2]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> There are 3 exits in this maze at [1,0], [0,2], and [2,3].\nInitially, you are at the entrance cell [1,2].\n- You can reach [1,0] by moving 2 steps left.\n- You can reach [0,2] by moving 1 step up.\nIt is impossible to reach [2,3] from the entrance.\nThus, the nearest exit is [0,2], which is 1 step away.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/06\/04\/nearesr2-grid.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> maze = [[\"+\",\"+\",\"+\"],[\".\",\".\",\".\"],[\"+\",\"+\",\"+\"]], entrance = [1,0]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There is 1 exit in this maze at [1,2].\n[1,0] does not count as an exit since it is the entrance cell.\nInitially, you are at the entrance cell [1,0].\n- You can reach [1,2] by moving 2 steps right.\nThus, the nearest exit is [1,2], which is 2 steps away.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/06\/04\/nearest3-grid.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> maze = [[\".\",\"+\"]], entrance = [0,0]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There are no exits in this maze.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>maze.length == m<\/code><\/li><li><code>maze[i].length == n<\/code><\/li><li><code>1 &lt;= m, n &lt;= 100<\/code><\/li><li><code>maze[i][j]<\/code>&nbsp;is either&nbsp;<code>'.'<\/code>&nbsp;or&nbsp;<code>'+'<\/code>.<\/li><li><code>entrance.length == 2<\/code><\/li><li><code>0 &lt;= entrance<sub>row<\/sub>&nbsp;&lt; m<\/code><\/li><li><code>0 &lt;= entrance<sub>col<\/sub>&nbsp;&lt; n<\/code><\/li><li><code>entrance<\/code>&nbsp;will always be an empty cell.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: BFS<\/strong><\/h2>\n\n\n\n<p>Use BFS to find the shortest path. We can re-use the board for visited array.<\/p>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {    \n    const int m = maze.size();\n    const int n = maze[0].size();\n    const vector<int> dirs{0, -1, 0, 1, 0};\n    queue<pair<int, int>> q;\n    q.emplace(entrance[1], entrance[0]);    \n    for (int steps = 0; !q.empty(); ++steps) {      \n      for (int s = q.size(); s; --s) {      \n        const auto [x, y] = q.front(); q.pop();        \n        if (x == 0 || x == n - 1 || y == 0 || y == m - 1)\n          if (x != entrance[1] || y != entrance[0])\n            return steps;\n        for (int i = 0; i < 4; ++i) {\n          const int tx = x + dirs[i];\n          const int ty = y + dirs[i + 1];\n          if (tx < 0 || tx >= n || ty < 0 || ty >= m || maze[ty][tx] != '.') continue;\n          maze[ty][tx] = '*';\n          q.emplace(tx, ty);\n        }\n      }      \n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an&nbsp;m x n&nbsp;matrix&nbsp;maze&nbsp;(0-indexed) with empty cells (represented as&nbsp;&#8216;.&#8217;) and walls (represented as&nbsp;&#8216;+&#8217;). You are also given the&nbsp;entrance&nbsp;of the maze, where&nbsp;entrance = [entrancerow,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[34,87],"class_list":["post-9285","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bfs","tag-shortest-path","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9285"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9285\/revisions"}],"predecessor-version":[{"id":9287,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9285\/revisions\/9287"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}