From 7a5650639f234fde7d15bdbc264d3c04ebc119ec Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 5 Jan 2022 19:35:40 +0800 Subject: [PATCH] =?UTF-8?q?2060.=20=E5=A5=B6=E7=89=9B=E9=80=89=E7=BE=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/9707232/ --- AcWing/2060/2060.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 AcWing/2060/2060.cpp diff --git a/AcWing/2060/2060.cpp b/AcWing/2060/2060.cpp new file mode 100644 index 00000000..f7f60f21 --- /dev/null +++ b/AcWing/2060/2060.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +using std::cin; +using std::cout; +using std::endl; + +const int N = 55; + +int n, m, k; +char g[N][N]; +std::vector> points[2]; + +void dfs(int x, int y, std::vector> &ps) { + if (x < 1 || x > n || y < 1 || y > m) return; + if (g[x][y] == '.') return; + g[x][y] = '.'; + ps.push_back(std::make_pair(x, y)); + dfs(x + 1, y, ps); + dfs(x - 1, y, ps); + dfs(x, y + 1, ps); + dfs(x, y - 1, ps); +} + +int main() { + cin >> n >> m; + for (int i = 1; i <= n; i++) { + cin >> g[i] + 1; + } + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + if (g[i][j] == 'X') { + dfs(i, j, points[k++]); + } + } + } + int ans = std::numeric_limits::max(); + for (auto a : points[0]) { + for (auto b : points[1]) { + ans = std::min(ans, std::abs(a.first - b.first) + std::abs(a.second - b.second) - 1); + } + } + cout << ans << endl; + return 0; +}