From abc26c5e5ab377379179dba63f93f7e2eef50353 Mon Sep 17 00:00:00 2001 From: StriveDD Date: Mon, 13 Mar 2023 21:26:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A01020.=E9=A3=9E=E6=8A=B5?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E9=87=8FJava=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1020.飞地的数量.md | 125 +++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/problems/1020.飞地的数量.md b/problems/1020.飞地的数量.md index 3da37376..ad848e73 100644 --- a/problems/1020.飞地的数量.md +++ b/problems/1020.飞地的数量.md @@ -144,6 +144,130 @@ public: } }; ``` +## 其他语言版本 + +**Java**: + +深度优先遍历版本: + +```java +class Solution { + // 四个方向 + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + + // 深度优先遍历,把可以通向边缘部分的 1 全部标记成 true + public void dfs(int[][] grid, int row, int col, boolean[][] visited) { + for (int[] current: position) { + int newRow = row + current[0], newCol = col + current[1]; + // 下标越界直接跳过 + if (newRow < 0 || newRow >= grid.length || newCol < 0 || newCol >= grid[0].length) continue; + // 当前位置不是 1 或者已经被访问了就直接跳过 + if (grid[newRow][newCol] != 1 || visited[newRow][newCol]) continue; + visited[newRow][newCol] = true; + dfs(grid, newRow, newCol, visited); + } + } + + public int numEnclaves(int[][] grid) { + int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案 + boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组 + // 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历 + for (int row = 0; row < rowSize; row++) { + if (grid[row][0] == 1 && !visited[row][0]) { + visited[row][0] = true; + dfs(grid, row, 0, visited); + } + if (grid[row][colSize - 1] == 1 && !visited[row][colSize - 1]) { + visited[row][colSize - 1] = true; + dfs(grid, row, colSize - 1, visited); + } + } + // 上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了 + for (int col = 1; col < colSize - 1; col++) { + if (grid[0][col] == 1 && !visited[0][col]) { + visited[0][col] = true; + dfs(grid, 0, col, visited); + } + if (grid[rowSize - 1][col] == 1 && !visited[rowSize - 1][col]) { + visited[rowSize - 1][col] = true; + dfs(grid, rowSize - 1, col, visited); + } + } + // 查找没有标记过的 1,记录到 ans 中 + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + if (grid[row][col] == 1 && !visited[row][col]) ++ans; + } + } + return ans; + } +} +``` + +广度优先遍历版本: + +```java +class Solution { + // 四个方向 + private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + + // 广度优先遍历,把可以通向边缘部分的 1 全部标记成 true + public void bfs(int[][] grid, Queue queue, boolean[][] visited) { + while (!queue.isEmpty()) { + int[] curPos = queue.poll(); + for (int[] current: position) { + int row = curPos[0] + current[0], col = curPos[1] + current[1]; + // 下标越界直接跳过 + if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) + continue; + // 当前位置不是 1 或者已经被访问了就直接跳过 + if (visited[row][col] || grid[row][col] == 0) continue; + visited[row][col] = true; + queue.add(new int[]{row, col}); + } + } + } + + public int numEnclaves(int[][] grid) { + int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案 + boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组 + Queue queue = new ArrayDeque<>(); + // 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历 + for (int row = 0; row < rowSize; row++) { + if (grid[row][0] == 1) { + visited[row][0] = true; + queue.add(new int[]{row, 0}); + } + if (grid[row][colSize - 1] == 1) { + visited[row][colSize - 1] = true; + queue.add(new int[]{row, colSize - 1}); + } + } + // 上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了 + for (int col = 1; col < colSize - 1; col++) { + if (grid[0][col] == 1) { + visited[0][col] = true; + queue.add(new int[]{0, col}); + } + if (grid[rowSize - 1][col] == 1 && !visited[rowSize - 1][col]) { + visited[rowSize - 1][col] = true; + queue.add(new int[]{rowSize - 1, col}); + } + } + bfs(grid, queue, visited); + // 查找没有标记过的 1,记录到 ans 中 + for (int row = 0; row < rowSize; row++) { + for (int col = 0; col < colSize; col++) { + if (grid[row][col] == 1 && !visited[row][col]) ++ans; + } + } + return ans; + } +} +``` + + + ## 类似题目 * 1254. 统计封闭岛屿的数目 @@ -153,3 +277,4 @@ public: +