forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
37 lines (37 loc) · 1.43 KB
/
Solution.cpp
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
class Solution {
public:
string findShortestWay(vector<vector<int>>& maze, vector<int>& ball, vector<int>& hole) {
int m = maze.size();
int n = maze[0].size();
int r = ball[0], c = ball[1];
int rh = hole[0], ch = hole[1];
queue<pair<int, int>> q;
q.push({r, c});
vector<vector<int>> dist(m, vector<int>(n, INT_MAX));
dist[r][c] = 0;
vector<vector<string>> path(m, vector<string>(n, ""));
vector<vector<int>> dirs = {{-1, 0, 'u'}, {1, 0, 'd'}, {0, -1, 'l'}, {0, 1, 'r'}};
while (!q.empty()) {
auto p = q.front();
q.pop();
int i = p.first, j = p.second;
for (auto& dir : dirs) {
int a = dir[0], b = dir[1];
char d = (char) dir[2];
int x = i, y = j;
int step = dist[i][j];
while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && maze[x + a][y + b] == 0 && (x != rh || y != ch)) {
x += a;
y += b;
++step;
}
if (dist[x][y] > step || (dist[x][y] == step && (path[i][j] + d < path[x][y]))) {
dist[x][y] = step;
path[x][y] = path[i][j] + d;
if (x != rh || y != ch) q.push({x, y});
}
}
}
return path[rh][ch] == "" ? "impossible" : path[rh][ch];
}
};