Repository navigation
Expand file tree
/
Copy path3568.cpp
More file actions
54 lines (53 loc) · 1.88 KB
/
Copy path3568.cpp
File metadata and controls
54 lines (53 loc) · 1.88 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
class Solution {
public:
int minMoves(vector<string>& classroom, int energy) {
int m = classroom.size();
int n = classroom[0].size();
vector<vector<vector<int>>> maxE(m, vector<vector<int>>(n, vector<int>(1025, -1)));
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
queue<vector<int>> q; // [x, y, mask, e, steps]
unordered_map<int, int> pos2LIdx;
int idx = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (classroom[i][j] == 'S') {
q.push({i, j, 0, energy, 0});
}
if (classroom[i][j] == 'L') {
pos2LIdx[i * 32 + j] = idx;
idx++;
}
}
}
int goal = (1 << idx) - 1;
while (!q.empty()) {
vector<int> v = q.front();
q.pop();
int x = v[0];
int y = v[1];
int mask = v[2];
int e = v[3];
int steps = v[4];
if (mask == goal) return steps;
if (e == 0) continue;
for (auto& direction : directions) {
int xx = x + direction.first;
int yy = y + direction.second;
int newMask = mask;
if (xx < 0 || xx >= m || yy < 0 || yy >= n) continue;
if (classroom[xx][yy] == 'X') continue;
int newE = e - 1;
if (classroom[xx][yy] == 'L') {
newMask |= (1 << pos2LIdx[xx * 32 + yy]);
}
if (classroom[xx][yy] == 'R') {
newE = energy;
}
if (newE <= maxE[xx][yy][mask]) continue;
maxE[xx][yy][newMask] = newE;
q.push({xx, yy, newMask, newE, steps + 1});
}
}
return -1;
}
};