864. Shortest Path to Get All Keys
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 K 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.
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 <= grid.length <= 301 <= grid[0].length <= 30grid[i][j]contains only'.','#','@','a'-'f'and'A'-'F'- The number of keys is in
[1, 6]. Each key has a different letter and opens exactly one lock.
Approach #1: C++. [BFS]
class Solution {
public:
int shortestPathAllKeys(vector<string>& grid) {
int m = grid.size();
int n = grid[0].size();
queue<int> q;
vector<vector<vector<int>>> seen(m, vector<vector<int>>(n, vector<int>(64, 0)));
int allKeys = 0;
//Init
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
const char c = grid[i][j];
if (c == '@') {
q.push((i << 16) | (j << 8));
seen[i][j][0] = 1;
} else if (c >= 'a' && c <= 'f') {
allKeys |= (1 << (c - 'a'));
}
}
}
int steps = 0;
vector<int> dirs = {-1, 0, 1, 0, -1};
while (!q.empty()) {
int size = q.size();
while (size--) {
int cur = q.front(); q.pop();
int x = cur >> 16;
int y = (cur >> 8) & 0xFF;
int keys = cur & 0xFF;
if (keys == allKeys) return steps;
for (int i = 0; i < 4; ++i) {
int xx = x + dirs[i];
int yy = y + dirs[i+1];
int curKeys = keys;
if (xx < 0 || xx >= m || yy < 0 || yy >= n) continue;
const char c = grid[xx][yy];
if (c == '#') continue;
if (c >= 'A' && c <= 'F' && !(keys & (1 << (c - 'A')))) continue;
if (c >= 'a' && c <= 'f') curKeys |= 1 << (c - 'a');
if (seen[xx][yy][curKeys]) continue;
seen[xx][yy][curKeys] = 1;
q.push((xx << 16) | (yy << 8) | curKeys);
}
}
steps++;
}
return -1;
}
};
Analysis:
seen[x][y][keys] : To store the position and the number of keys. If this state don't be traveled we can do next step, otherwise we skip this state.
allKeys : To represent the keys which we will collect in this problem. In this problem we use six binary numbers to represent all the keys at difference bit.
such as : a -> 1 so it will be represented by 000001 and f -> f - 'a' = 6 so it will be represent by 100000. If we have the keys of a and f so we can use 100001 to represent that.
queue<int> q : To simulation the BFS.
864. Shortest Path to Get All Keys的更多相关文章
- [LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...
- [Swift]LeetCode864. 获取所有钥匙的最短路径 | Shortest Path to Get All Keys
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...
- 最短路径遍历所有的节点 Shortest Path Visiting All Nodes
2018-10-06 22:04:38 问题描述: 问题求解: 本题要求是求遍历所有节点的最短路径,由于本题中是没有要求一个节点只能访问一次的,也就是说可以访问一个节点多次,但是如果表征两次节点状态呢 ...
- [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 ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
随机推荐
- linux那点事儿(七)----文件系统管理
如果你是一位忠实的windows 用户,那么现在请你打开的的c盘,打开WINDWOS目录,下面存放了哪些文件和目录,相信没有人关心过吧!即便是用windows多年的人.额!其实,我也知道WINDOWS ...
- spring-boot多环境配置文件
spring-boot多环境配置文件 目录 配置 多环境配置文件名称要遵循格式 application-{profile}.yml application.yml spring: profiles: ...
- stm32下载程序,拔了调试器不能运行程序解决方案
A:肯定是只拔了仿真器与电脑连接的那端,然后把另外端依然接在板子上.我说的没错吧 B: 对的,这样就会一直复位吗 这是复位的问题,当JLINK在板子上连接的时候,断电情况下,会一直把RESET拉低,导 ...
- srvctl和crs_start命令无法启动oracle RAC实例, 但sqlplus可以启动
今天遇到一个奇怪问题,发现srvctl和crs_start命令无法启动Oracle RAC实例,但用sqlplus却可以正常启动.最终发现原因是在OCR中数据库的状态变成了disable,将此状态更改 ...
- [原创]JMeter初次使用总结
引言 最近开发 java 后端项目,对外提供Restful API接口,完整功能开发现已完成. 目前通过单测(68%行覆盖率)已保证业务逻辑正确性,同时也尝试使用JMeter进行压力测试以保证并发性能 ...
- 音频AAC编码浅析
/* * unsigned long nSampleRate, // 采样率,单位是bps * unsigned long nChannels, // 声道,1为单声道,2为双声道 * unsigne ...
- mfs教程(一)
对于mfs文件系统也用了半年了,确实不错,最近又翻译了作者的三篇文章,再此一同发上,希望对大家有所帮助.不足之处还请指出,以便完善,谢谢! 感谢网友nonamexz做了精美的pdf文档 MFS文件系统 ...
- 微信小程序怎么获取用户输入
能够获取用户输入的组件,需要使用组件的属性bindchange将用户的输入内容同步到 AppService. <input id="myInput" bindchange=& ...
- hadoop 2.7.3 (hadoop2.x)使用ant制作eclipse插件hadoop-eclipse-plugin-2.7.3.jar
为了做mapreduce开发,要使用eclipse,并且需要对应的Hadoop插件hadoop-eclipse-plugin-2.7.3.jar,首先说明一下,在hadoop1.x之前官方hadoop ...
- GNU 和 g++(转)
百度知道 GNU计划,又称革奴计划,是由Richard Stallman在1983年9月27日公开发起的.它的目标是创建一套完全自由的操作系统.Richard Stallman最早是在net.unix ...