Shortest Path to Get All Keys - LeetCode
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 <= 30
1 <= grid[0].length <= 30
grid[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.
状态压缩+哈希+BFS
v1:用string类型的哈希记录走过的状态, 212ms
struct State{
int keys, x, y; // keys代表当前状态(点x,y)拥有的钥匙的个数
State(int keys, int x, int y):keys(keys),x(x),y(y){}
};
class Solution {
public:
int shortestPathAllKeys(vector<string>& grid) {
int n = grid.size(), m = grid[0].size();
int sx = -1, sy = -1, statemax = -1;
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++) {
char c = grid[i][j];
if (c == '@') sx = i, sy = j;
// if K = 6, i.e. exist 'F' -> statemax = 111111
if (c >= 'a' && c <= 'f') statemax = max(statemax |= 1 << (c - 'a'), statemax);
}
State start(0, sx, sy);
queue<State> q;
unordered_set<string> hash;
hash.insert(to_string(0) + " " + to_string(sx) + " " + to_string(sy));
q.push(start);
int Next[4][2] = {0,1,0,-1,1,0,-1,0};
int step = 0;
// 遍历当前step
while (!q.empty()) {
int size = q.size();
while (size --) {
State cur = q.front();
q.pop();
for (int k = 0; k < 4; k ++ ) {
int x = cur.x, y = cur.y, keys = cur.keys;
int nx = x + Next[k][0];
int ny = y + Next[k][1];
if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
char c = grid[nx][ny];
if (c == '#') continue;
if (c >= 'A' && c <= 'F' && ((keys >> (c - 'A')) & 1) == 0 ) continue;
if (c >= 'a' && c <= 'f') keys |= 1 << (c - 'a');
if (!hash.count(to_string(keys) + " " + to_string(nx) + " " + to_string(ny))) {
if (cur.keys == statemax) return step;
hash.insert(to_string(keys) + " " + to_string(nx) + " " + to_string(ny));
q.push(State(keys, nx, ny));
}
}
}
}
step ++;
}
return -1;
}
};
v2:用数组记录走过的状态,20ms
struct State{
int keys, x, y; // keys代表当前状态(点x,y)拥有的钥匙的个数
State(int keys, int x, int y):keys(keys),x(x),y(y){}
};
class Solution {
public:
unordered_set<char> hash;
// 111111 = 63
//1000000 = 64
bool vis[30][30][65];
int totState = 0;
int Next[4][2] = {0,1,0,-1,1,0,-1,0};
int shortestPathAllKeys(vector<string>& grid) {
int n = grid.size(), m = grid[0].size();
int sx = -1, sy = -1;
for (int i = 0; i < n; i ++ ) {
for (int j = 0; j < m; j ++ ) {
if (grid[i][j] == '@') sx = i, sy = j;
if (grid[i][j] >= 'a' && grid[i][j] <= 'f') totState = max(totState, totState |= 1 << (grid[i][j] - 'a'));
}
}
queue<State> q;
q.push(State(0, sx, sy));
vis[sx][sy][0] = true;
int step = 0;
while (!q.empty()) {
int size = q.size();
while (size -- ) {
State cur = q.front();
q.pop();
for (int k = 0; k < 4; k ++ ) {
int x = cur.x, y = cur.y, keys = cur.keys;
int nx = x + Next[k][0];
int ny = y + Next[k][1];
if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
char c = grid[x][y];
if (c == '#') continue;
else if (c >= 'A' && c <= 'F' && !(keys >> (c - 'a') & 1)) continue;
else if (c >= 'a' && c <= 'f') keys |= 1 << (c - 'a');
if (!vis[nx][ny][keys]) {
if (keys == totState) return step;
vis[nx][ny][keys] = true;
q.push(State(keys, nx, ny));
}
}
}
}
step ++;
}
return -1;
}
};