We are given a 2-dimensional `grid`. `"."` is an empty cell, `"#"` is a wall, `"@"` is the starting point, (`"a"`, `"b"`, ...) are keys, and (`"A"`, `"B"`, ...) are locks.

We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key.

For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first Kletters 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.

Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.

Example 1:

Input: ["@.a.#","###.#","b.A.B"]
Output: 8

Example 2:

Input: ["@..aA","..B#.","....b"]
Output: 6

Note:

  1. 1 <= grid.length <= 30
  2. 1 <= grid[0].length <= 30
  3. grid[i][j] contains only '.''#''@''a'-``'f``' and 'A'-'F'
  4. The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.

这道题给了我们一个迷宫,其中的点表示可通过的位置,井号表示墙,不能通过。小写字母表示钥匙,大写字母表示门,只有拿到了对应的钥匙才能通过门的地方,有点红白机游戏的感觉。问我们收集到所有的钥匙需要的最小步数,当无法收到所有钥匙的时候,返回 -1。对于迷宫遍历找最小步数的题,应该并不陌生,基本都是用 BFS 来解的。这里虽然没有一个固定的终点位置,但也是有终止条件的,那就是收集到所有的钥匙。这道题好就好在对钥匙进行了一些限定,最多有6把,最少有1把,而且都是按字母顺序出现的,就是说只有一把钥匙的时候,一定是a,两把的话一定是a和b。我们需要保存所有当前已经获得的钥匙,并且还要随时查询是否已经获得了某个特定的钥匙,还需要查询是否已经获得了所有的钥匙。由于之前说了知道了钥匙的个数,就能确定是哪些钥匙,这样就可以对钥匙进行编号,钥匙a编为0,同理,b,c,d,e,f 分别编为 1,2,3,4,5。最简单的实现就是用一个长度为k的 boolean 数组,获得了某个钥匙就标记为 true,查询某个钥匙是否存在就直接在数组中对应位置查询即可,判断是否获得所有钥匙就线性遍历一下数组即可,由于最多就6把钥匙,所以遍历也很快。当然,若我们想秀一波技巧,也可以将钥匙编码成二进制数,对应位上的0和1表示该钥匙是存在,比如二进制数 111111 就表示六把钥匙都有了,而 100111 就表示有钥匙 a,d,e 和f,这样查询某个钥匙或查询所有钥匙的时间复杂度都是常数级了,既省了空间又省了时间,岂不美哉?!

分析到这里,基本上难点都 cover 了,可以准备写代码了。整体框架还是经典的 BFS 写法,再稍加改动即可。这里的队列 queue 不能只放位置信息,还需要放当前的钥匙信息,因为到达不同的位置获得的钥匙个数可能是不同的,二维的位置信息编码成一个整数,再加上钥匙的整数,组成一个 pair 对儿放到队列中。由于参数中没有事先告诉我们起点的位置,所以需要先遍历一遍整个迷宫,找到@符号,将位置和当前钥匙信息加入到 queue 中。为了避免死循环,BFS 遍历是需要记录已经访问过的位置的,这里的状态当然也要加入当前钥匙的信息,为了简单起见,将其编码成一个字符串,前半部分放位置编码成的整数,中间加个下划线,后面放钥匙信息的整数,组成的字符串放到 HashSet 中即可。遍历的过程中同时还要统计钥匙的个数,有了总个数 keyCnt,就能知道拿到所有钥匙后编码成的整数。在 while 循环,采用层序遍历的机制,对于同一层的结点,分别取出位置信息和钥匙信息,此时先判断下是否已经拿到所有钥匙了,是的话直接返回当前步数 res。否则就要检测其四个相邻位置,需要注意的是,对于每个相邻位置,一定要重新取出之前的钥匙信息,否则一旦钥匙信息修改了而没有重置的话,直接到同一层的其他结点可能会引起错误。取出的邻居结点的位置要先判断是否越界,还要判断是否为墙,是的话就直接跳过。若是门的话,要看当前是否有该门对应的钥匙,有的话才能通过。若遇到了钥匙,则需要修改钥匙信息。这些都完成了之后,将当前的位置和钥匙信息编码成一个字符串,看 HashSet 是否已经有了这个状态,没有的话,则加入 HashSet,并同时加入 queue,每当一层结点遍历完成后,结果 res 自增1即可,参见代码如下:

```
class Solution {
public:
int shortestPathAllKeys(vector& grid) {
int m = grid.size(), n = grid[0].size(), keyCnt = 0, res = 0;
queue> q;
unordered_set visited;
vector dirX{-1, 0, 1, 0}, dirY{0, 1, 0, -1};
for (int i = 0; i = 'a' && grid[i][j] 0; --i) {
int t = q.front().first, curKeys = q.front().second; q.pop();
if (curKeys == (1 = m || y = n) continue;
char c = grid[x][y];
if (c == '#') continue;
if (c >= 'A' && c > (c - 'A')) & 1) == 0) continue;
if (c >= 'a' && c
Github 同步地址:

https://github.com/grandyang/leetcode/issues/864

参考资料:

https://leetcode.com/problems/shortest-path-to-get-all-keys/

https://leetcode.com/problems/shortest-path-to-get-all-keys/discuss/146878/Java-BFS-Solution

https://leetcode.com/problems/shortest-path-to-get-all-keys/discuss/146941/C%2B%2B-BFS-with-current-key-recorded-visited-map-(12ms)

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径的更多相关文章

  1. 864. Shortest Path to Get All Keys

    We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...

  2. [LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

  3. LeetCode 1091. Shortest Path in Binary Matrix

    原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/ 题目: In an N by N square grid, ...

  4. [Swift]LeetCode864. 获取所有钥匙的最短路径 | Shortest Path to Get All Keys

    We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...

  5. LeetCode 847. Shortest Path Visiting All Nodes

    题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...

  6. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  7. LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination

    题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) ...

  8. leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径

    设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...

  9. [MIT6.006] 19. Daynamic Programming I: Fibonacci, Shortest Path 动态规划I:斐波那契,最短路径

    这节课讲动态规划的内容,动态规划是一种通用且有效的算法设计思路,它的主要成分是"子问题"+"重用".它可以用于斐波那契和最短路径等问题的求解上. 一.斐波那契 ...

随机推荐

  1. MongoDB自学------(2)创建删除数据库及集合

    一.创建数据库 二.查看所有数据库 三.删除数据库 四.创建集合 五.删除集合 六.集合用法介绍 1.创建集合 2.删除集合 下一篇链接:https://www.cnblogs.com/LinHuCh ...

  2. 关于Idea突然无法输入的诡异问题解决

    问题描述 最近加班把自己的装有Debian的笔记本带到公司,使用Idea写代码的时候,突然间无法输入,ctrl与tab还可用,重启Idea能得到一阵的解决 解决参考 如果是Linux平台,请考虑是否是 ...

  3. Window权限维持(二):计划任务

    Windows操作系统提供了一个实用程序(schtasks.exe),使系统管理员能够在特定的日期和时间执行程序或脚本.这种行为可作为一种持久性机制被red team利用.通过计划任务执行持久性不需要 ...

  4. 基本认证(Basic Authorization)

    ---------------------------------- import arcpy from base64 import encodestring username = 'xxx' pas ...

  5. 算法笔记 第6章 C++标准模版库(STL)介绍 学习笔记

    6.1 vector的常见用法详解 vector:变长数组,长度根据需要而自动改变的数组 要使用vector,则需要添加vector头文件,即#include<vector>,还需要在头文 ...

  6. Vue笔记3

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 使用highcharts实现无其他信息纯趋势图实战实例

    使用highcharts实现无其他信息纯趋势图实战实例 Highcharts去掉或者隐藏掉y轴的刻度线yAxis : { gridLineWidth: 0, labels:{ //enabled:fa ...

  8. Web点击链接调起手机系统自带短信发短信

    实现代码如下: 一.Html代码 <a href="javascript:;" class="xq-sms">发送短信</a> 二.jQ ...

  9. 在vue里使用codemirror的两种用法

    这是我自己做的一个左边点击对应的标题,右边显示相应代码的一个功能.代码显示这里用的是vue-codemirror插件. 第一种用法: 1.安装:npm install vue-codemirror - ...

  10. map、filter、reduce函数的使用

    1.filter() 作用:过滤 // 1.筛选出大于30的数. const array = [10, 20, 30, 40, 50, 60, 70, 80] // 普通写法 // let newar ...