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: ...
随机推荐
- python学习(三) 使用字符串
第三章 使用字符串 ...
- BigDecimal进行精确运算demo工具类
package com.js.ai.modules.pointwall.interfac; import java.math.BigDecimal; public class TestDigDecim ...
- Python——通过斐波那契数列来理解生成器
一.生成器(generator) 先来看看一个简单的菲波那切数列,出第一个和第二个外,任意一个数都是由前两个数相加得到的.如:0,1,1,2,3,5,8,13...... 输入斐波那契数列前N个数: ...
- MySql主从复制原理和环境配置搭建
主从复制原理 实质就是通过二进制的sql文件实现主从复制 MySQL的主从复制是MySQL本身自带的一个功能,不需要额外的第三方软件就可以实现,其复制功能并不是copy文件来实现的,而是借助binlo ...
- SpringMVC总结四:拦截器简单介绍
首先要说一下HandlerExecutionChain: HandlerExecutionChain是一个执行链,当用户的请求到达DispatcherServlet的时候,DispatcherServ ...
- Redis安装文档
1.前置条件 前置条件:linux已经可以上网,参考:https://www.cnblogs.com/ZenoLiang/p/10201875.html 2.安装redis 2.1依赖包检查 1. ...
- 介绍个好点的,JAVA技术群
java技术交流,意义是以QQ群为媒介,添加一些有多年工作经验和技术的人群,为有问题的人群解答在工作中遇到的各种问题为思想,java技术交流群号161571685,创建时间为2010年,走过将近5年的 ...
- opencv3读取视频并保存为图片
#include <iostream> #include <vector> #include <opencv2/opencv.hpp> using namespac ...
- ubuntu16.04 Mask_RCNN AlphaPose OpenPose Librealsense
#############MaskRCNNcource activate flappbirdcd /home/luo/Desktop/MyFile/MaskRCNN/MyOwnMaskRCNN1/sa ...
- Cunit编译安装
1. Examples/Makefile.am:26: to 'configure.ac' and run 'autoconf' again. configure.ac:211: error: re ...