-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.cpp
83 lines (75 loc) · 1.8 KB
/
main.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
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
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
int n = 0, m = 0;
vector<vector<int>> grid, cost;
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
void _calculateCost(int row, int col) {
vector<vector<bool>> seen(n, vector<bool>(m, false));
queue<tuple<int, int, int>> q;
q.push(make_tuple(row, col, 0));
while(!q.empty()) {
int r = 0, c = 0, depth = 0;
tie(r, c, depth) = q.front();
q.pop();
for (auto d: directions) {
int r1 = r + d.first, c1 = c + d.second;
if (r1 >= 0 && r1 < n && c1 >=0 && c1 < m &&
!seen[r1][c1] && grid[r1][c1] == 0) {
cost[r1][c1] += depth + 1;
q.push(make_tuple(r1, c1, depth + 1));
seen[r1][c1] = true;
}
}
}
}
public:
int shortestDistance(vector<vector<int>> &grid) {
n = grid.size();
if (n == 0) {
return 0;
}
m = grid[0].size();
this->grid = grid;
this->cost = vector<vector<int>>(n, vector<int>(m, 0));
for(int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1) {
_calculateCost(i, j);
}
}
}
int ans = INT_MAX;
for(int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 0) {
ans = min(ans, cost[i][j]);
}
}
}
return ans == INT_MAX ? -1 : ans;
}
};
int main() {
Solution sol;
vector<vector<int>> grid = {
{1, 0, 2, 0, 1},
{0, 0, 0, 0, 0},
{0, 0, 1, 0, 0},
};
cout << sol.shortestDistance(grid) << endl;
return 0;
}