1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-03-17 01:32:25 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/originData/shortest-path-to-get-all-keys.json

168 lines
22 KiB
JSON
Raw Normal View History

2022-03-27 20:46:41 +08:00
{
"data": {
"question": {
"questionId": "895",
"questionFrontendId": "864",
"categoryTitle": "Algorithms",
"boundTopicId": 1915,
"title": "Shortest Path to Get All Keys",
"titleSlug": "shortest-path-to-get-all-keys",
"content": "<p>You are given an <code>m x n</code> grid <code>grid</code> where:</p>\n\n<ul>\n\t<li><code>&#39;.&#39;</code> is an empty cell.</li>\n\t<li><code>&#39;#&#39;</code> is a wall.</li>\n\t<li><code>&#39;@&#39;</code> is the starting point.</li>\n\t<li>Lowercase letters represent keys.</li>\n\t<li>Uppercase letters represent locks.</li>\n</ul>\n\n<p>You start at the starting point and one move consists of walking one space in one of the four cardinal directions. You cannot walk outside the grid, or walk into a wall.</p>\n\n<p>If you walk over a key, you can pick it up and you cannot walk over a lock unless you have its corresponding key.</p>\n\n<p>For some <code><font face=\"monospace\">1 &lt;= k &lt;= 6</font></code>, there is exactly one lowercase and one uppercase letter of the first <code>k</code> letters of the English alphabet in the grid. This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet.</p>\n\n<p>Return <em>the lowest number of moves to acquire all keys</em>. If it is impossible, return <code>-1</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/23/lc-keys2.jpg\" style=\"width: 404px; height: 245px;\" />\n<pre>\n<strong>Input:</strong> grid = [&quot;@.a.#&quot;,&quot;###.#&quot;,&quot;b.A.B&quot;]\n<strong>Output:</strong> 8\n<strong>Explanation:</strong> Note that the goal is to obtain all the keys not to open all the locks.\n</pre>\n\n<p><strong>Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/23/lc-key2.jpg\" style=\"width: 404px; height: 245px;\" />\n<pre>\n<strong>Input:</strong> grid = [&quot;@..aA&quot;,&quot;..B#.&quot;,&quot;....b&quot;]\n<strong>Output:</strong> 6\n</pre>\n\n<p><strong>Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/23/lc-keys3.jpg\" style=\"width: 244px; height: 85px;\" />\n<pre>\n<strong>Input:</strong> grid = [&quot;@Aa&quot;]\n<strong>Output:</strong> -1\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>m == grid.length</code></li>\n\t<li><code>n == grid[i].length</code></li>\n\t<li><code>1 &lt;= m, n &lt;= 30</code></li>\n\t<li><code>grid[i][j]</code> is either an English letter, <code>&#39;.&#39;</code>, <code>&#39;#&#39;</code>, or <code>&#39;@&#39;</code>.</li>\n\t<li>The number of keys in the grid is in the range <code>[1, 6]</code>.</li>\n\t<li>Each key in the grid is <strong>unique</strong>.</li>\n\t<li>Each key in the grid has a matching lock.</li>\n</ul>\n",
"translatedTitle": "获取所有钥匙的最短路径",
"translatedContent": "<p>给定一个二维网格&nbsp;<code>grid</code>&nbsp;,其中:</p>\n\n<ul>\n\t<li><font color=\"#c7254e\"><font face=\"Menlo, Monaco, Consolas, Courier New, monospace\"><span style=\"font-size:12.6px\"><span style=\"background-color:#f9f2f4\">'.'</span></span></font></font> 代表一个空房间</li>\n\t<li><font color=\"#c7254e\"><font face=\"Menlo, Monaco, Consolas, Courier New, monospace\"><span style=\"font-size:12.6px\"><span style=\"background-color:#f9f2f4\">'#'</span></span></font></font> 代表一堵</li>\n\t<li><font color=\"#c7254e\"><font face=\"Menlo, Monaco, Consolas, Courier New, monospace\"><span style=\"font-size:12.6px\"><span style=\"background-color:#f9f2f4\">'@'</span></span></font></font>&nbsp;是起点</li>\n\t<li>小写字母代表钥匙</li>\n\t<li>大写字母代表锁</li>\n</ul>\n\n<p>我们从起点开始出发,一次移动是指向四个基本方向之一行走一个单位空间。我们不能在网格外面行走,也无法穿过一堵墙。如果途经一个钥匙,我们就把它捡起来。除非我们手里有对应的钥匙,否则无法通过锁。</p>\n\n<p>假设 k&nbsp;为 钥匙/锁 的个数,且满足&nbsp;<code>1 &lt;= k&nbsp;&lt;= 6</code>,字母表中的前 <code>k</code>&nbsp;个字母在网格中都有自己对应的一个小写和一个大写字母。换言之,每个锁有唯一对应的钥匙,每个钥匙也有唯一对应的锁。另外,代表钥匙和锁的字母互为大小写并按字母顺序排列。</p>\n\n<p>返回获取所有钥匙所需要的移动的最少次数。如果无法获取所有钥匙,返回&nbsp;<code>-1</code>&nbsp;。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1</strong></p>\n\n<p><img src=\"https://assets.leetcode.com/uploads/2021/07/23/lc-keys2.jpg\" /></p>\n\n<pre>\n<strong>输入:</strong>grid = [\"@.a.#\",\"###.#\",\"b.A.B\"]\n<strong>输出:</strong>8\n<strong>解释:</strong>目标是获得所有钥匙,而不是打开所有锁。\n</pre>\n\n<p><strong>示例 2</strong></p>\n\n<p><img src=\"https://assets.leetcode.com/uploads/2021/07/23/lc-key2.jpg\" /></p>\n\n<pre>\n<strong>输入:</strong>grid = [\"@..aA\",\"..B#.\",\"....b\"]\n<strong>输出:</strong>6\n</pre>\n\n<p><strong>示例 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/23/lc-keys3.jpg\" />\n<pre>\n<strong>输入:</strong> grid = [\"@Aa\"]\n<strong>输出:</strong> -1</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>m == grid.length</code></li>\n\t<li><code>n == grid[i].length</code></li>\n\t<li><code>1 &lt;= m, n &lt;= 30</code></li>\n\t<li><code>grid[i][j]</code>&nbsp;只含有&nbsp;<code>'.'</code>,&nbsp;<code>'#'</code>,&nbsp;<code>'@'</code>,&nbsp;<code>'a'-</code><code>'f</code><code>'</code>&nbsp;以及&nbsp;<code>'A'-'F'</code></li>\n\t<li>钥匙的数目范围是&nbsp;<code>[1, 6]</code>&nbsp;</li>\n\t<li>每个钥匙都对应一个 <strong>不同</strong> 的字母</li>\n\t<li>每个钥匙正好打开一个对应的锁</li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 102,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": false, \"java\": true, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false}",
"topicTags": [
{
"name": "Bit Manipulation",
"slug": "bit-manipulation",
"translatedName": "位运算",
"__typename": "TopicTagNode"
},
{
"name": "Breadth-First Search",
"slug": "breadth-first-search",
"translatedName": "广度优先搜索",
"__typename": "TopicTagNode"
}
],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "C++",
"langSlug": "cpp",
"code": "class Solution {\npublic:\n int shortestPathAllKeys(vector<string>& grid) {\n\n }\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Java",
"langSlug": "java",
"code": "class Solution {\n public int shortestPathAllKeys(String[] grid) {\n\n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python",
"langSlug": "python",
"code": "class Solution(object):\n def shortestPathAllKeys(self, grid):\n \"\"\"\n :type grid: List[str]\n :rtype: int\n \"\"\"",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python3",
"langSlug": "python3",
"code": "class Solution:\n def shortestPathAllKeys(self, grid: List[str]) -> int:",
"__typename": "CodeSnippetNode"
},
{
"lang": "C",
"langSlug": "c",
"code": "\n\nint shortestPathAllKeys(char ** grid, int gridSize){\n\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "C#",
"langSlug": "csharp",
"code": "public class Solution {\n public int ShortestPathAllKeys(string[] grid) {\n\n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {string[]} grid\n * @return {number}\n */\nvar shortestPathAllKeys = function(grid) {\n\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Ruby",
"langSlug": "ruby",
"code": "# @param {String[]} grid\n# @return {Integer}\ndef shortest_path_all_keys(grid)\n\nend",
"__typename": "CodeSnippetNode"
},
{
"lang": "Swift",
"langSlug": "swift",
"code": "class Solution {\n func shortestPathAllKeys(_ grid: [String]) -> Int {\n\n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Go",
"langSlug": "golang",
"code": "func shortestPathAllKeys(grid []string) int {\n\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Scala",
"langSlug": "scala",
"code": "object Solution {\n def shortestPathAllKeys(grid: Array[String]): Int = {\n\n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Kotlin",
"langSlug": "kotlin",
"code": "class Solution {\n fun shortestPathAllKeys(grid: Array<String>): Int {\n\n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Rust",
"langSlug": "rust",
"code": "impl Solution {\n pub fn shortest_path_all_keys(grid: Vec<String>) -> i32 {\n\n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "PHP",
"langSlug": "php",
"code": "class Solution {\n\n /**\n * @param String[] $grid\n * @return Integer\n */\n function shortestPathAllKeys($grid) {\n\n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function shortestPathAllKeys(grid: string[]): number {\n\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Racket",
"langSlug": "racket",
"code": "(define/contract (shortest-path-all-keys grid)\n (-> (listof string?) exact-integer?)\n\n )",
"__typename": "CodeSnippetNode"
},
{
"lang": "Erlang",
"langSlug": "erlang",
"code": "-spec shortest_path_all_keys(Grid :: [unicode:unicode_binary()]) -> integer().\nshortest_path_all_keys(Grid) ->\n .",
"__typename": "CodeSnippetNode"
},
{
"lang": "Elixir",
"langSlug": "elixir",
"code": "defmodule Solution do\n @spec shortest_path_all_keys(grid :: [String.t]) :: integer\n def shortest_path_all_keys(grid) do\n\n end\nend",
"__typename": "CodeSnippetNode"
}
],
2022-05-02 23:44:12 +08:00
"stats": "{\"totalAccepted\": \"3.8K\", \"totalSubmission\": \"8K\", \"totalAcceptedRaw\": 3757, \"totalSubmissionRaw\": 7998, \"acRate\": \"47.0%\"}",
2022-03-27 20:46:41 +08:00
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[\"@.a.#\",\"###.#\",\"b.A.B\"]",
"metaData": "{\r\n \"name\": \"shortestPathAllKeys\",\r\n \"params\": [\r\n {\r\n \"name\": \"grid\",\r\n \"type\": \"string[]\"\r\n }\r\n ],\r\n \"return\": {\r\n \"type\": \"integer\"\r\n }\r\n}\r\n",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"cpp\":[\"C++\",\"<p>\\u7248\\u672c\\uff1a<code>clang 11<\\/code> \\u91c7\\u7528\\u6700\\u65b0C++ 17\\u6807\\u51c6\\u3002<\\/p>\\r\\n\\r\\n<p>\\u7f16\\u8bd1\\u65f6\\uff0c\\u5c06\\u4f1a\\u91c7\\u7528<code>-O2<\\/code>\\u7ea7\\u4f18\\u5316\\u3002<a href=\\\"https:\\/\\/github.com\\/google\\/sanitizers\\/wiki\\/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer<\\/a> \\u4e5f\\u88ab\\u5f00\\u542f\\u6765\\u68c0\\u6d4b<code>out-of-bounds<\\/code>\\u548c<code>use-after-free<\\/code>\\u9519\\u8bef\\u3002<\\/p>\\r\\n\\r\\n<p>\\u4e3a\\u4e86\\u4f7f\\u7528\\u65b9\\u4fbf\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8\\u5bfc\\u5165\\u3002<\\/p>\"],\"java\":[\"Java\",\"<p>\\u7248\\u672c\\uff1a<code>OpenJDK 17<\\/code>\\u3002\\u53ef\\u4ee5\\u4f7f\\u7528Java 8\\u7684\\u7279\\u6027\\u4f8b\\u5982\\uff0clambda expressions \\u548c stream API\\u3002<\\/p>\\r\\n\\r\\n<p>\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u88ab\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n<p>\\u5305\\u542b Pair \\u7c7b: https:\\/\\/docs.oracle.com\\/javase\\/8\\/javafx\\/api\\/javafx\\/util\\/Pair.html <\\/p>\"],\"python\":[\"Python\",\"<p>\\u7248\\u672c\\uff1a <code>Python 2.7.12<\\/code><\\/p>\\r\\n\\r\\n<p>\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u5e38\\u7528\\u5e93\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8 \\u5bfc\\u5165\\uff0c\\u5982\\uff1a<a href=\\\"https:\\/\\/docs.python.org\\/2\\/library\\/array.html\\\" target=\\\"_blank\\\">array<\\/a>, <a href=\\\"https:\\/\\/docs.python.org\\/2\\/library\\/bisect.html\\\" target=\\\"_blank\\\">bisect<\\/a>, <a href=\\\"https:\\/\\/docs.python.org\\/2\\/library\\/collections.html\\\" target=\\\"_blank\\\">collections<\\/a>\\u3002\\u5982\\u679c\\u60a8\\u9700\\u8981\\u4f7f\\u7528\\u5176\\u4ed6\\u5e93\\u51fd\\u6570\\uff0c\\u8bf7\\u81ea\\u884c\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n<p>\\u6ce8\\u610f Python 2.7 <a href=\\\"https:\\/\\/www.python.org\\/dev\\/peps\\/pep-0373\\/\\\" target=\\\"_blank\\\">\\u5c06\\u57282020\\u5e74\\u540e\\u4e0d\\u518d\\u7ef4\\u62a4<\\/a>\\u3002 \\u5982\\u60f3\\u4f7f\\u7528\\u6700\\u65b0\\u7248\\u7684Python\\uff0c\\u8bf7\\u9009\\u62e9Python 3\\u3002<\\/p>\"],\"c\":[\"C\",\"<p>\\u7248\\u672c\\uff1a<code>GCC 8.2<\\/code>\\uff0c\\u91c7\\u7528GNU99\\u6807\\u51c6\\u3002<\\/p>\\r\\n\\r\\n<p>\\u7f16\\u8bd1\\u65f6\\uff0c\\u5c06\\u4f1a\\u91c7\\u7528<code>-O1<\\/code>\\u7ea7\\u4f18\\u5316\\u3002 <a href=\\\"https:\\/\\/github.com\\/google\\/sanitizers\\/wiki\\/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer<\\/a>\\u4e5f\\u88ab\\u5f00\\u542f\\u6765\\u68c0\\u6d4b<code>out-of-bounds<\\/code>\\u548c<code>use-after-free<\\/code>\\u9519\\u8bef\\u3002<\\/p>\\r\\n\\r\\n<p>\\u4e3a\\u4e86\\u4f7f\\u7528\\u65b9\\u4fbf\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n<p>\\u5982\\u60f3\\u4f7f\\u7528\\u54c8\\u5e0c\\u8868\\u8fd0\\u7b97, \\u60a8\\u53ef\\u4ee5\\u4f7f\\u7528 <a href=\\\"https:\\/\\/troydhanson.github.io\\/uthash\\/\\\" target=\\\"_blank\\\">uthash<\\/a>\\u3002 \\\"uthash.h\\\"\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5bfc\\u5165\\u3002\\u8bf7\\u770b\\u5982\\u4e0b\\u793a\\u4f8b:<\\/p>\\r\\n\\r\\n<p><b>1. \\u5f80\\u54c8\\u5e0c\\u8868\\u4e2d\\u6dfb\\u52a0\\u4e00\\u4e2a\\u5bf9\\u8c61\\uff1a<\\/b>\\r\\n<pre>\\r\\nstruct hash_entry {\\r\\n int id; \\/* we'll use this field as the key *\\/\\r\\n char name[10];\\r\\n UT_hash_handle hh; \\/* makes this structure hashable *\\/\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n<\\/pre>\\r\\n<\\/p>\\r\\n\\r\\n<p><b>2. \\u5728\\u54c8\\u5e0c\\u8868\\u4e2d\\u67e5\\u627e\\u4e00\\u4e2a\\u5bf9\\u8c61\\uff1a<\\/b>\\r\\n<pre>\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n re
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[\"@.a.#\",\"###.#\",\"b.A.B\"]\n[\"@..aA\",\"..B#.\",\"....b\"]\n[\"@Aa\"]",
"__typename": "QuestionNode"
}
}
}