-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
minimum-time-takes-to-reach-destination-without-drowning.cpp
103 lines (101 loc) · 3.87 KB
/
minimum-time-takes-to-reach-destination-without-drowning.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Time: O(m * n)
// Space: O(m * n)
// simulation, bfs
class Solution {
public:
int minimumSeconds(vector<vector<string>>& land) {
static const vector<pair<int, int>> DIRECTIONS {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<vector<int>> lookup(size(land), vector<int>(size(land[0])));
vector<tuple<int, int, int>> q;
int i0 = -1, j0 = -1;
for (int i = 0; i < size(land); ++i) {
for (int j = 0; j < size(land[0]); ++j) {
if (land[i][j] == "*") {
q.emplace_back(i, j, -1);
lookup[i][j] = -1;
} else if (land[i][j] == "S") {
i0 = i, j0 = j;
}
}
}
q.emplace_back(i0, j0, 1);
lookup[i0][j0] = 1;
while (!empty(q)) {
vector<tuple<int, int, int>> new_q;
for (const auto& [i, j, d] : q) {
if (land[i][j] == "D") {
return d - 1;
}
for (const auto& [di, dj] : DIRECTIONS) {
const int ni = i + di, nj = j + dj;
if (!(0 <= ni && ni < size(land) && 0 <= nj && nj < size(land[0]) && land[ni][nj] != "X" && lookup[ni][nj] != -1)) {
continue;
}
if (d != -1 && lookup[ni][nj] == 0) {
lookup[ni][nj] = 1;
new_q.emplace_back(ni, nj, d + 1);
} else if (d == -1 && land[ni][nj] != "D") {
lookup[ni][nj] = -1;
new_q.emplace_back(ni, nj, -1);
}
}
}
q = move(new_q);
}
return -1;
}
};
// Time: O(m * n)
// Space: O(m * n)
// simulation, bfs
class Solution2 {
public:
int minimumSeconds(vector<vector<string>>& land) {
static const vector<pair<int, int>> DIRECTIONS {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<vector<int>> lookup1(size(land), vector<int>(size(land[0]), -1));
vector<vector<int>> lookup2(size(land), vector<int>(size(land[0]), -1));
vector<pair<int, int>> q1;
vector<pair<int, int>> q2;
for (int i = 0; i < size(land); ++i) {
for (int j = 0; j < size(land[0]); ++j) {
if (land[i][j] == "*") {
q1.emplace_back(i, j);
lookup2[i][j] = 0;
} else if (land[i][j] == "S") {
q2.emplace_back(i, j);
lookup2[i][j] = 0;
}
}
}
while (!empty(q1) || !empty(q2)) {
vector<pair<int, int>> new_q1;
vector<pair<int, int>> new_q2;
for (const auto& [i, j] : q1) {
for (const auto& [di, dj] : DIRECTIONS) {
const int ni = i + di, nj = j + dj;
if (!(0 <= ni && ni < size(land) && 0 <= nj && nj < size(land[0]) && land[ni][nj] != "X" && land[ni][nj] != "D" && lookup1[ni][nj] == -1)) {
continue;
}
lookup1[ni][nj] = 0;
new_q1.emplace_back(ni, nj);
}
}
for (const auto& [i, j] : q2) {
if (land[i][j] == "D") {
return lookup2[i][j];
}
for (const auto& [di, dj] : DIRECTIONS) {
const int ni = i + di, nj = j + dj;
if (!(0 <= ni && ni < size(land) && 0 <= nj && nj < size(land[0]) && land[ni][nj] != "X" && lookup2[ni][nj] == -1 && lookup1[ni][nj] == -1)) {
continue;
}
lookup2[ni][nj] = lookup2[i][j] + 1;
new_q2.emplace_back(ni, nj);
}
}
q1 = move(new_q1);
q2 = move(new_q2);
}
return -1;
}
};