-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum-steps-to-reach.cpp
79 lines (66 loc) · 2.18 KB
/
minimum-steps-to-reach.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
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef pair<int, int> Pos;
int minimum_steps(const vector<vector<bool>>& m, pair<int, int> s, pair<int, int> e) {
int row = m.size();
int col = m[0].size();
vector<vector<int>> distance(row, vector<int>(col, -1));
distance[s.first][s.second] = 0;
vector<Pos> st;
vector<Pos> next_st;
int step = 1;
st.push_back(s);
int limit = row * col;
while (distance[e.first][e.second] == -1 && step < limit) {
while (!st.empty()) {
auto b = st.back(); st.pop_back();
// right
Pos p = make_pair(b.first, b.second+1);
if (p.second < col && !m[p.first][p.second]) {
if (distance[p.first][p.second] == -1) {
distance[p.first][p.second] = step;
next_st.push_back(p);
}
}
// left
p = make_pair(b.first, b.second-1);
if (p.second >= 0 && !m[p.first][p.second]) {
if (distance[p.first][p.second] == -1) {
distance[p.first][p.second] = step;
next_st.push_back(p);
}
}
// top
p = make_pair(b.first-1, b.second);
if (p.first >= 0 && !m[p.first][p.second]) {
if (distance[p.first][p.second] == -1) {
distance[p.first][p.second] = step;
next_st.push_back(p);
}
}
// bottom
p = make_pair(b.first+1, b.second);
if (p.first < row && !m[p.first][p.second]) {
if (distance[p.first][p.second] == -1) {
distance[p.first][p.second] = step;
next_st.push_back(p);
}
}
}
st.swap(next_st);
++step;
}
return distance[e.first][e.second];
}
int main() {
vector<vector<bool>> m{
{false, false, false, false},
{true, true, false, true},
{false, false, false, false},
{false, false, false, false}
};
cout << minimum_steps(m, {3 ,0}, {0, 0}) << endl;
return 0;
}