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
|
#include <algorithm>
#include <iostream>
#include <set>
#include <queue>
using std::vector;
struct P {
int x;
int y;
};
struct I {
int x;
int y;
int v;
};
P mm[]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
class Solution {
public:
vector<vector<int>> bicycleYard(vector<int> &position,
vector<vector<int>> &terrain,
vector<vector<int>> &obstacle) {
std::vector<std::vector<std::set<int>>> visited(
terrain.size(), std::vector<std::set<int>>(terrain.front().size()));
std::queue<I> q{{position[0], position[1], 1}};
bool f = false;
while (true) {
if (f && q.size() == 1) {
break;
}
f = true;
I b = q.back();
q.pop_back();
if (b.v <= 0 || visited[b.x][b.y].count(b.v)) {
}
visited[b.x][b.y].insert(v);
for (auto p : mm) {
int nx = x + p.x, ny = y + p.y;
if (nx >= 0 && nx < terrain.size() && ny >= 0 &&
ny < terrain.front().size()) {
DFS(v + terrain[x][y] - terrain[nx][ny] - obstacle[nx][ny], nx, ny,
terrain, obstacle, visited);
}
}
}
vector<vector<int>> result;
for (int i = 0; i < terrain.size(); i++) {
for (int j = 0; j < obstacle.size(); j++) {
if ((i != position[0] || j != position[1]) && visited[i][j].count(1)) {
result.push_back({i, j});
}
}
}
return result;
}
};
|