# 1020. 飞地的数量 给你一个大小为 m x n 的二进制矩阵 grid ,其中 0 表示一个海洋单元格、1 表示一个陆地单元格。 一次 移动 是指从一个陆地单元格走到另一个相邻(上、下、左、右)的陆地单元格或跨过 grid 的边界。 返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830100710.png) * 输入:grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]] * 输出:3 * 解释:有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830100742.png) * 输入:grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]] * 输出:0 * 解释:所有 1 都在边界上或可以到达边界。 ## 思路 本题使用dfs,bfs,并查集都是可以的。 本题和 417. 太平洋大西洋水流问题 很像。 本题要求找到不靠边的陆地面积,那么我们只要从周边找到陆地然后 通过 dfs或者bfs 将周边靠陆地且相邻的陆地都变成海洋,然后再去重新遍历地图的时候,统计此时还剩下的陆地就可以了。 如图,在遍历地图周围四个边,靠地图四边的陆地,都为绿色, ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104632.png) 在遇到地图周边陆地的时候,将1都变为0,此时地图为这样: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220830104651.png) 然后我们再去遍历这个地图,遇到有陆地的地方,去采用深搜或者广搜,边统计所有陆地。 如果对深搜或者广搜不够了解,建议先看这里:[深度优先搜索精讲](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/),[广度优先搜索精讲](https://leetcode.cn/circle/discuss/V3FulB/) 采用深度优先搜索的代码如下: ```CPP class Solution { private: int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; // 保存四个方向 int count; // 统计符合题目要求的陆地空格数量 void dfs(vector>& grid, int x, int y) { grid[x][y] = 0; count++; for (int i = 0; i < 4; i++) { // 向四个方向遍历 int nextx = x + dir[i][0]; int nexty = y + dir[i][1]; // 超过边界 if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 不符合条件,不继续遍历 if (grid[nextx][nexty] == 0) continue; dfs (grid, nextx, nexty); } return; } public: int numEnclaves(vector>& grid) { int n = grid.size(), m = grid[0].size(); // 从左侧边,和右侧边 向中间遍历 for (int i = 0; i < n; i++) { if (grid[i][0] == 1) dfs(grid, i, 0); if (grid[i][m - 1] == 1) dfs(grid, i, m - 1); } // 从上边和下边 向中间遍历 for (int j = 0; j < m; j++) { if (grid[0][j] == 1) dfs(grid, 0, j); if (grid[n - 1][j] == 1) dfs(grid, n - 1, j); } count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] == 1) dfs(grid, i, j); } } return count; } }; ``` 采用广度优先搜索的代码如下: ```CPP class Solution { private: int count = 0; int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向 void bfs(vector>& grid, int x, int y) { queue> que; que.push({x, y}); grid[x][y] = 0; // 只要加入队列,立刻标记 count++; while(!que.empty()) { pair cur = que.front(); que.pop(); int curx = cur.first; int cury = cur.second; for (int i = 0; i < 4; i++) { int nextx = curx + dir[i][0]; int nexty = cury + dir[i][1]; if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过 if (grid[nextx][nexty] == 1) { que.push({nextx, nexty}); count++; grid[nextx][nexty] = 0; // 只要加入队列立刻标记 } } } } public: int numEnclaves(vector>& grid) { int n = grid.size(), m = grid[0].size(); // 从左侧边,和右侧边 向中间遍历 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); } count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] == 1) bfs(grid, i, j); } } return count; } }; ```