From 75c2df61c9b34785e12571228530c3bd4fc2ad23 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Wed, 1 Oct 2025 21:02:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E5=9F=BA=E4=BA=8E0-1?= =?UTF-8?q?=20BFS=E7=9A=84=E6=9C=80=E5=A4=A7=E6=AC=A7=E5=BC=8F=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加0-1 BFS算法来计算网格中可达点的最小代价,并在此基础上计算满足条件的两点间最大欧式距离。主要功能包括: - 实现0-1 BFS算法计算各点到起点的最小代价 - 遍历所有点对,筛选满足代价条件的点对 - 计算并输出最大欧式距离 --- src/10/1/P4162.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/src/10/1/P4162.cpp b/src/10/1/P4162.cpp index 294989d..3e2e5d6 100644 --- a/src/10/1/P4162.cpp +++ b/src/10/1/P4162.cpp @@ -1,3 +1,89 @@ -int main(){ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +const int MAXN = 35; +const int dx[4] = {0, 1, 0, -1}; +const int dy[4] = {1, 0, -1, 0}; + +int n, m, T; +char grid[MAXN][MAXN]; + +bool isValid(int x, int y) { + return x >= 0 && x < n && y >= 0 && y < m; +} + +vector> bfs01(int sx, int sy) { + vector> dist(n, vector(m, 0x3f3f3f3f)); + vector> visited(n, vector(m, false)); + deque> dq; -} \ No newline at end of file + int startcost = (grid[sx][sy] == '1') ? 1 : 0; + dist[sx][sy] = startcost; + dq.push_back({sx, sy}); + + while (!dq.empty()) { + auto [x, y] = dq.front(); + dq.pop_front(); + + if (visited[x][y]) continue; + visited[x][y] = true; + + for (int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (!isValid(nx, ny)) continue; + + int cost = (grid[nx][ny] == '1') ? 1 : 0; + if (dist[x][y] + cost < dist[nx][ny]) { + dist[nx][ny] = dist[x][y] + cost; + if (cost == 0) { + dq.push_front({nx, ny}); + } else { + dq.push_back({nx, ny}); + } + } + } + } + + return dist; +} + +double euladis(int x1, int y1, int x2, int y2) { + return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m >> T; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + + double maxdis = 0.0; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + auto dist = bfs01(i, j); + for (int x = 0; x < n; x++) { + for (int y = 0; y < m; y++) { + if (dist[x][y] <= T) { + maxdis = max(maxdis, euladis(i, j, x, y)); + } + } + } + } + } + printf("%.6f\n", maxdis); +}