From a16ad4990037c4488158762221cbe2c719e37642 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Wed, 25 Sep 2024 11:29:11 +0800 Subject: [PATCH] od: Add cpp impl to moving area --- src/od/2024e200/moving-area-of-robot/Makefile | 6 ++ src/od/2024e200/moving-area-of-robot/index.md | 14 ++- .../moving-area-of-robot/solution.cpp | 89 +++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/od/2024e200/moving-area-of-robot/Makefile create mode 100644 src/od/2024e200/moving-area-of-robot/solution.cpp diff --git a/src/od/2024e200/moving-area-of-robot/Makefile b/src/od/2024e200/moving-area-of-robot/Makefile new file mode 100644 index 00000000..98586cd8 --- /dev/null +++ b/src/od/2024e200/moving-area-of-robot/Makefile @@ -0,0 +1,6 @@ + +build: solution.cpp + g++ -g solution.cpp -o solution + +clean: + rm solution diff --git a/src/od/2024e200/moving-area-of-robot/index.md b/src/od/2024e200/moving-area-of-robot/index.md index 8a2bc69b..741b162a 100644 --- a/src/od/2024e200/moving-area-of-robot/index.md +++ b/src/od/2024e200/moving-area-of-robot/index.md @@ -62,4 +62,16 @@ ## 题解 -使用 广度优先搜索 BFS 求得最大的连接节点数. \ No newline at end of file +使用 广度优先搜索 BFS 求得最大的连接节点数. + +### Python + +```python +{{#include solution.py:6:}} +``` + +### C++ + +```cpp +{{#include solution.cpp:5:}} +``` diff --git a/src/od/2024e200/moving-area-of-robot/solution.cpp b/src/od/2024e200/moving-area-of-robot/solution.cpp new file mode 100644 index 00000000..b5ceae07 --- /dev/null +++ b/src/od/2024e200/moving-area-of-robot/solution.cpp @@ -0,0 +1,89 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +#include + +#include +#include +#include +#include + +int dfs_visit(const std::vector>& grid, + std::vector>& visited, + int x, int y, int rows, int columns) { + if (visited[x][y]) { + return 0; + } + // 先标记当前节点状态 + int move_range = 1; + visited[x][y] = true; + + const std::array, 4> directions = + {std::array{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + // 开始准备向四个方向访问 + for (const auto dir : directions) { + int x1 = x + dir[0]; + int y1 = y + dir[1]; + // 判断新的节点是否满足条件 + // 1. 在矩形范围内移动 + // 2. 新的节点未被访问过 + // 3. 两个节点上的值相差小于等于1 + + if (0 <= x1 && x1 < rows && 0 <= y1 && y1 < columns && !visited[x1][y1] && + std::abs(grid[x][y] - grid[x1][y1]) <= 1) { + // 递归访问新的节点 + move_range += dfs_visit(grid, visited, x1, y1, rows, columns); + } + } + + // 返回移动次数 + return move_range; +} + +void solution() { + // 读取行与列 + int rows = 0; + int columns = 0; + std::cin >> rows >> columns; + assert(rows > 0 && columns > 0); + // 换行符 + std::string line; + std::getline(std::cin, line); + + // 读取二维数组 + std::vector> grid; + for (int row = 0; row < rows; ++row) { + std::vector nums(columns); + for (int& num : nums) { + std::cin >> num; + } + grid.emplace_back(nums); + // 换行符 + std::getline(std::cin, line); + } + + // 标记已经访问过了的节点 + std::vector> visited(rows, std::vector(columns, false)); + + int max_move_range = 0; + + // 接下来遍历所有的节点, 找到最大的访问范围. + for (int row = 0; row < rows; ++row) { + for (int column = 0; column < columns; ++column) { + // 如果该节点已经访问过, 就忽略 + if (!visited[row][column]) { + const int move_range = dfs_visit(grid, visited, row, column, rows, columns); + max_move_range = std::max(max_move_range, move_range); + } + } + } + + // 打印结果 + std::cout << max_move_range << std::endl; +} + +int main() { + solution(); + return 0; +}