diff options
author | crupest <crupest@outlook.com> | 2021-09-27 20:00:08 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-09-27 20:00:08 +0800 |
commit | 88fcda225127be5fe52cfd235c673ae7a744f7bb (patch) | |
tree | 88409ddf55d90a47412d7ae13642b45c46028026 /leetcode/2.cpp | |
parent | 87feba5d6ba7f7f8ff2c67164e2415d24f7eff89 (diff) | |
download | solutions-88fcda225127be5fe52cfd235c673ae7a744f7bb.tar.gz solutions-88fcda225127be5fe52cfd235c673ae7a744f7bb.tar.bz2 solutions-88fcda225127be5fe52cfd235c673ae7a744f7bb.zip |
Diffstat (limited to 'leetcode/2.cpp')
-rw-r--r-- | leetcode/2.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/leetcode/2.cpp b/leetcode/2.cpp new file mode 100644 index 0000000..4cf9c73 --- /dev/null +++ b/leetcode/2.cpp @@ -0,0 +1,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; + } +};
\ No newline at end of file |