-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshortest-distance.cc
86 lines (76 loc) · 2.93 KB
/
shortest-distance.cc
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// https://leetcode.com/problems/shortest-distance-from-all-buildings/description/
typedef pair<int,int> coord;
class Solution {
private:
void VisitSurroundings(pair<coord, int> c, vector<vector<int> >& grid,
vector<vector<bool> >& visited,
vector<vector<int> >& times_visited,
queue<pair<coord, int>>& to_explore,
vector<vector<int> >& distance) {
int rows = grid.size();
int cols = grid[0].size();
int i = c.first.first;
int j = c.first.second;
for (int k = i - 1; k <= i + 1; ++k) {
for (int q = j - 1; q <= j + 1; ++q) {
if (0 <= k && k < rows && 0 <= q && q < cols &&
abs(k - i) + abs(q - j) <= 1) {
if (grid[k][q] == 0 && !visited[k][q]) {
to_explore.push({{k, q}, c.second + 1});
distance[k][q] += c.second + 1;
++times_visited[k][q];
visited[k][q] = true;
}
}
}
}
}
public:
int shortestDistance(vector<vector<int>>& grid) {
queue<pair<coord, int> > to_explore;
vector<vector<int> > times_visited(grid.size(),
vector<int>(grid[0].size(), 0));
vector<vector<int> > distance(grid.size(),
vector<int>(grid[0].size(), 0));
vector<vector<bool> > visited(grid.size(),
vector<bool>(grid[0].size(), false));
int result = -1;
int num_buildings = 0;
// Drop a person walking out of each building
// to see distance to rest of the grid
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[i].size(); ++j) {
if (grid[i][j] == 1) {
++num_buildings;
// reset everything to not visited
for (auto& row : visited)
fill(row.begin(), row.end(), false);
VisitSurroundings({{i,j}, 0}, grid, visited,
times_visited, to_explore,
distance);
while(!to_explore.empty()) {
auto c = to_explore.front();
to_explore.pop();
VisitSurroundings(c, grid, visited,
times_visited,
to_explore, distance);
}
}
}
}
// Compute best solution
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[i].size(); ++j) {
// If we can build in current spot and can be reached from all the buildings,
// update best solution
if (grid[i][j] == 0 && times_visited[i][j] == num_buildings) {
if(result == -1)
result = distance[i][j];
else
result = min(result, distance[i][j]);
}
}
}
return result;
}
};