-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestPathtoGetAllKeys.cpp
More file actions
53 lines (52 loc) · 1.85 KB
/
shortestPathtoGetAllKeys.cpp
File metadata and controls
53 lines (52 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Source: https://leetcode.com/problems/shortest-path-to-get-all-keys/
// Author: Miao Zhang
// Date: 2021-03-19
class Solution {
public:
int shortestPathAllKeys(vector<string>& grid) {
int m = grid.size();
int n = grid[0].size();
int all_keys = 0;
queue<int> q;
vector<vector<vector<int>>> seen(m, vector<vector<int>>(n, vector<int>(64, 0)));
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') {
all_keys |= (1 << (c - 'a'));
}
}
}
vector<vector<int>> dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int steps = 0;
while (!q.empty()) {
int len = q.size();
while (len--) {
int s = q.front();
q.pop();
int i = s >> 16;
int j = (s >> 8) & 0xFF;
int keys = s & 0xFF;
if (keys == all_keys) return steps;
for (auto& d: dirs) {
int x = i + d[0];
int y = j + d[1];
int nkeys = keys;
if (x < 0 || x >= m || y < 0 || y >= n) continue;
char c = grid[x][y];
if (c == '#') continue;
if (c >= 'A' && c <= 'F' && !(keys & (1 << (c - 'A')))) continue;
if (c >= 'a' && c <= 'f') nkeys |= (1 << (c - 'a'));
if (seen[x][y][nkeys]) continue;
q.push((x << 16) | (y << 8) | nkeys);
seen[x][y][nkeys] = 1;
}
}
steps++;
}
return -1;
}
};