From ae959ea8e7ccb67c910a6dda46a328a2b098aa25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=BA=E7=94=9F=E7=9C=8B=E6=B7=A1=E4=B8=8D=E6=9C=8D?= =?UTF-8?q?=E5=B0=B1=E5=B9=B2?= <1295875288@qq.com> Date: Sat, 13 Dec 2025 08:49:41 +0000 Subject: [PATCH] =?UTF-8?q?update=20experiments/exp06/G.cpp.=20=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=A8=A1=E6=8B=9F=E4=B8=8D=E6=98=93=E7=90=86=E8=A7=A3?= =?UTF-8?q?=E4=B8=94=E7=B9=81=E7=90=90=EF=BC=8C=E9=89=B4=E4=BA=8E=E5=AD=A6?= =?UTF-8?q?=E7=94=9F=E6=B0=B4=E5=B9=B3=E5=8F=82=E5=B7=AE=E4=B8=8D=E9=BD=90?= =?UTF-8?q?=E3=80=81=E9=AB=98=E4=BD=8E=E8=90=BD=E5=B7=AE=E5=A4=A7=EF=BC=8C?= =?UTF-8?q?=E7=89=B9=E6=AD=A4=E5=86=99=E5=87=BA=E8=83=8E=E6=95=99=E7=89=88?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 人生看淡不服就干 <1295875288@qq.com> --- experiments/exp06/G.cpp | 67 +++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/experiments/exp06/G.cpp b/experiments/exp06/G.cpp index 01c79ad..18134f7 100644 --- a/experiments/exp06/G.cpp +++ b/experiments/exp06/G.cpp @@ -1,43 +1,46 @@ -// -// Created by webturing on 2024/1/1. -// -#include - +#include +#include using namespace std; -int g[100][100]; -int n, m; //n点的个数 m边的个数 -int book[100]; -int tot = 0; -int q[100 * 100], head = 0, tail = -1; - -void bfs(int k) { - head = 0, tail = -1; - q[++tail] = k; - book[k] = 1; - while (head <= tail) { +int n; +int g[100][100], book[100]; //g是邻接矩阵,book是标记数组,标记是否访问过 +queue q; - int h = q[head++]; +void bfs(){ + /* bfs模板:(无脑套就行) + 先入队 + 后while(队列不空){ + 取队首元素,这个就是目标元素了 + 然后探索,用for探索周边最近的节点 + if剪枝判断是否符合,符合条件则放入队列 + } + + */ + q.push(0); + book[0] = 1; + while (!q.empty()) + { + int h = q.front(); + q.pop(); cout << h << " "; - for (int i = 0; i < n; i++) { - if (book[i] == 0 && g[h][i] == 1) { - q[++tail] = i; + book[h] = 1; + for (int i = 0; i < n; i++) + { + if(!book[i] && g[h][i]){ + q.push(i); book[i] = 1; } } + } - + } - -int main() { +int main(){ cin >> n; - for (int x = 0; x < n; x++) - for (int y = 0; y < n; y++) { - cin >> g[x][y]; - m += g[x][y]; + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + cin >> g[i][j]; } - fill(book, book + n, 0); - bfs(0); - return 0; -} - + } + bfs(); +} \ No newline at end of file -- Gitee