From 31a93f398d55d974555a0b4bd49fc39a3bba562b Mon Sep 17 00:00:00 2001 From: Yornii <2571425925@qq.com> Date: Sun, 2 Nov 2025 13:46:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200099.=E5=B2=9B=E5=B1=BF=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E5=B9=BF=E6=90=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Yornii <2571425925@qq.com> --- ...6\225\260\351\207\217\345\271\277\346\220\234.md" | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index cfa6af18e4..a456b8b0d6 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -199,13 +199,13 @@ public class Main { public static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};//下右上左逆时针遍历 public static void bfs(int[][] grid, boolean[][] visited, int x, int y) { - Queue queue = new LinkedList();//定义坐标队列,没有现成的pair类,在下面自定义了 - queue.add(new pair(x, y)); + Queue queue = new LinkedList<>();//定义坐标队列,没有现成的Pair类,在下面自定义了 + queue.add(new Pair(x, y)); visited[x][y] = true;//遇到入队直接标记为优先, // 否则出队时才标记的话会导致重复访问,比如下方节点会在右下顺序的时候被第二次访问入队 while (!queue.isEmpty()) { - int curX = queue.peek().first; - int curY = queue.poll().second;//当前横纵坐标 + int curX = queue.peek().x(); + int curY = queue.poll().y();//当前横纵坐标 for (int i = 0; i < 4; i++) { //顺时针遍历新节点next,下面记录坐标 int nextX = curX + dir[i][0]; @@ -214,7 +214,7 @@ public class Main { continue; }//去除越界部分 if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) { - queue.add(new pair(nextX, nextY)); + queue.add(new Pair(nextX, nextY)); visited[nextX][nextY] = true;//逻辑同上 } } @@ -244,6 +244,8 @@ public class Main { System.out.println(ans); } } + +record Pair(int x,int y){} ``` From 78c74c72c0ce3f5087c862c45f35ef2805239328 Mon Sep 17 00:00:00 2001 From: Yornii <2571425925@qq.com> Date: Sun, 2 Nov 2025 20:58:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200102.=E6=B2=89=E6=B2=A1=E5=AD=A4?= =?UTF-8?q?=E5=B2=9B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Yornii <2571425925@qq.com> --- ...11\346\262\241\345\255\244\345\262\233.md" | 81 ++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" index e335a018f5..8e3e1dafd5 100644 --- "a/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" +++ "b/problems/kamacoder/0102.\346\262\211\346\262\241\345\255\244\345\262\233.md" @@ -140,8 +140,8 @@ int main() { ### Java +深搜版 ```JAVA - import java.util.Scanner; public class Main { @@ -206,7 +206,86 @@ public class Main { ``` +广搜版 +```java +import java.util.*; + +public class Main { + static int[][] dir = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} }; // 上、左、下、右 + + public static void bfs(int[][] grid, int startX, int startY) { + int n = grid.length; + int m = grid[0].length; + Queue queue = new LinkedList<>(); + queue.offer(new int[]{startX, startY}); + grid[startX][startY] = 2; // 标记为已访问 + + while (!queue.isEmpty()) { + int[] curr = queue.poll(); + int x = curr[0]; + int y = curr[1]; + + for (int[] d : dir) { + int nextX = x + d[0]; + int nextY = y + d[1]; + + // 越界检查 + if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= m) continue; + // 不符合条件:不是 1 的格子不处理 + if (grid[nextX][nextY] != 1) continue; + + grid[nextX][nextY] = 2; // 标记访问 + queue.offer(new int[]{nextX, nextY}); + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] grid = new int[n][m]; + + // 读入数据 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + grid[i][j] = scanner.nextInt(); + } + } + + // 步骤一:从四条边界开始 BFS + // 左右边界 + for (int i = 0; i < n; i++) { + if (grid[i][0] == 1) bfs(grid, i, 0); + if (grid[i][m - 1] == 1) bfs(grid, i, m - 1); + } + + // 上下边界 + for (int j = 0; j < m; j++) { + if (grid[0][j] == 1) bfs(grid, 0, j); + if (grid[n - 1][j] == 1) bfs(grid, n - 1, j); + } + + // 步骤二:更新网格 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 1) grid[i][j] = 0; // 被包围的区域 + if (grid[i][j] == 2) grid[i][j] = 1; // 恢复边界连通区域 + } + } + + // 输出结果 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + System.out.print(grid[i][j] + " "); + } + System.out.println(); + } + scanner.close(); + } +} +``` ### Python