From 6ddccfd8a7e377c8eb9a34f4f15b6f51eec947de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B8=96=E4=BB=BB?= <3289741869@qq.com> Date: Wed, 5 Nov 2025 22:49:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?topic6=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\345\274\240\344\270\226\344\273\273.cpp" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 "topic06/submit/LC934_\345\274\240\344\270\226\344\273\273.cpp" diff --git "a/topic06/submit/LC934_\345\274\240\344\270\226\344\273\273.cpp" "b/topic06/submit/LC934_\345\274\240\344\270\226\344\273\273.cpp" new file mode 100644 index 0000000..6ce04a0 --- /dev/null +++ "b/topic06/submit/LC934_\345\274\240\344\270\226\344\273\273.cpp" @@ -0,0 +1,56 @@ +#include +using namespace std; + +class Solution { +public: + int shortestBridge(vector>& grid) { + int m = grid.size(), n = grid.size(); + queue> q; + bool found = false; + for (int i = 0; i < m && !found; i++) { // 原:i=1 + for (int j = 0; j < n && !found; j++) { // 原:j=1 + if (grid[i][j] == 1) { // 原:gird[i][j] + dfs(grid, i, j, q); // 原:gird,且末尾无; + found = true; + } + } + } + + int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + int steps = 0; + while (!q.empty()) { // 原:q.empty.() + int size = q.size(); + for (int i = 1; i <= size; i++) { + auto [x, y] = q.front(); // 原:atuo[x,y] + q.pop(); + for (auto& d : dirs) { // 原:for(atuo& d;dirs) + int nx = x + d[0], ny = y + d[1]; + if (nx >= 0 && nx < m && ny >= 0 && ny < n) { // 原:nx<0 && nx>m... + if (grid[nx][ny] == 1) { // 正确判断另一座岛 + return steps; + } + if (grid[nx][ny] == 0) { // 原:gird + grid[nx][ny] = 2; // 标记为已访问 + q.push({nx, ny}); + } + } + } + } + steps++; + } + return -1; + } + +private: + void dfs(vector>& grid, int i, int j, queue>& q) { + if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() || grid[i][j] != 1) { + return; + } + grid[i][j] = 2; // 标记第一座岛 + q.push({i, j}); + dfs(grid, i + 1, j, q); + dfs(grid, i - 1, j, q); + dfs(grid, i, j + 1, q); + dfs(grid, i, j - 1, q); + } +}; \ No newline at end of file -- Gitee From 2c98118b2034fad5a3f90fd1c1bce255f364404c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B8=96=E4=BB=BB?= <3289741869@qq.com> Date: Thu, 6 Nov 2025 19:17:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?topic6=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\345\274\240\344\270\226\344\273\273.cpp" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "topic06/submit/LC429_\345\274\240\344\270\226\344\273\273.cpp" diff --git "a/topic06/submit/LC429_\345\274\240\344\270\226\344\273\273.cpp" "b/topic06/submit/LC429_\345\274\240\344\270\226\344\273\273.cpp" new file mode 100644 index 0000000..717aca0 --- /dev/null +++ "b/topic06/submit/LC429_\345\274\240\344\270\226\344\273\273.cpp" @@ -0,0 +1,45 @@ +/* +// Definition for a Node. +class Node { +public: + int val; + vector children; + + Node() {} + + Node(int _val) { + val = _val; + } + + Node(int _val, vector _children) { + val = _val; + children = _children; + } +}; +*/ + +class Solution { +public: + vector> levelOrder(Node* root) { + vector> res; + if(root==nullptr){ + return res; + } + queue que; + que.push(root); + while(!que.empty()){ + int n=que.size(); + vector nodes; + for(int i=1;i<=n;i++){ + Node* node=que.front(); + nodes.emplace_back(node->val); + que.pop(); + for(Node* child :node->children){ + que.push(child); + } + } + res.emplace_back(nodes); + } + return res; + } +}; \ No newline at end of file -- Gitee