1254:统计封闭岛屿的数目

This commit is contained in:
huangge1199 2021-07-30 14:49:09 +08:00
parent 5346b60d90
commit 597448a9fc
2 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,117 @@
//有一个二维矩阵 grid 每个位置要么是陆地记号为 0 要么是水域记号为 1
//
// 我们从一块陆地出发每次可以往上下左右 4 个方向相邻区域走能走到的所有陆地区域我们将其称为一座岛屿
//
// 如果一座岛屿 完全 由水域包围即陆地边缘上下左右所有相邻区域都是水域那么我们将其称为 封闭岛屿
//
// 请返回封闭岛屿的数目
//
//
//
// 示例 1
//
//
//
// 输入grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1
//,0,1],[1,1,1,1,1,1,1,0]]
//输出2
//解释
//灰色区域的岛屿是封闭岛屿因为这座岛屿完全被水域包围即被 1 区域包围
//
// 示例 2
//
//
//
// 输入grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
//输出1
//
//
// 示例 3
//
// 输入grid = [[1,1,1,1,1,1,1],
//  [1,0,0,0,0,0,1],
//  [1,0,1,1,1,0,1],
//  [1,0,1,0,1,0,1],
//  [1,0,1,1,1,0,1],
//  [1,0,0,0,0,0,1],
// [1,1,1,1,1,1,1]]
//输出2
//
//
//
//
// 提示
//
//
// 1 <= grid.length, grid[0].length <= 100
// 0 <= grid[i][j] <=1
//
// Related Topics 深度优先搜索 广度优先搜索 并查集 数组 矩阵
// 👍 83 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TwoArray;
//1254:统计封闭岛屿的数目
public class NumberOfClosedIslands {
public static void main(String[] args) {
//测试代码
Solution solution = new NumberOfClosedIslands().new Solution();
TwoArray twoArray = new TwoArray("" +
"[[1,1,1,1,1,1,1,0]," +
"[1,0,0,0,0,1,1,0]," +
"[1,0,1,0,1,1,1,0]," +
"[1,0,0,0,0,1,0,1]," +
"[1,1,1,1,1,1,1,0]]"
);
System.out.println(solution.closedIsland(twoArray.getArr()));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
boolean[][] use;
int[] dx = new int[]{-1, 1, 0, 0};
int[] dy = new int[]{0, 0, -1, 1};
int count = 0;
boolean flag;
public int closedIsland(int[][] grid) {
use = new boolean[grid.length][grid[0].length];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 0 && !use[i][j]) {
flag = true;
dfs(i, j, grid);
if (flag) {
count++;
}
}
}
}
return count;
}
private void dfs(int x, int y, int[][] grid) {
use[x][y] = true;
for (int i = 0; i < 4; i++) {
x += dx[i];
y += dy[i];
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) {
x -= dx[i];
y -= dy[i];
flag = false;
continue;
}
if (grid[x][y] == 0 && !use[x][y]) {
dfs(x, y, grid);
}
x -= dx[i];
y -= dy[i];
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,48 @@
<p>有一个二维矩阵 <code>grid</code>&nbsp;,每个位置要么是陆地(记号为&nbsp;<code>0</code> )要么是水域(记号为&nbsp;<code>1</code> )。</p>
<p>我们从一块陆地出发,每次可以往上下左右&nbsp;4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座「<strong>岛屿</strong>」。</p>
<p>如果一座岛屿&nbsp;<strong>完全</strong>&nbsp;由水域包围,即陆地边缘上下左右所有相邻区域都是水域,那么我们将其称为 「<strong>封闭岛屿</strong>」。</p>
<p>请返回封闭岛屿的数目。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/07/sample_3_1610.png"></p>
<pre><strong>输入:</strong>grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
<strong>输出:</strong>2
<strong>解释:</strong>
灰色区域的岛屿是封闭岛屿,因为这座岛屿完全被水域包围(即被 1 区域包围)。</pre>
<p><strong>示例 2</strong></p>
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/07/sample_4_1610.png"></p>
<pre><strong>输入:</strong>grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
<strong>输出:</strong>1
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>grid = [[1,1,1,1,1,1,1],
&nbsp; [1,0,0,0,0,0,1],
&nbsp; [1,0,1,1,1,0,1],
&nbsp; [1,0,1,0,1,0,1],
&nbsp; [1,0,1,1,1,0,1],
&nbsp; [1,0,0,0,0,0,1],
[1,1,1,1,1,1,1]]
<strong>输出:</strong>2
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= grid.length, grid[0].length &lt;= 100</code></li>
<li><code>0 &lt;= grid[i][j] &lt;=1</code></li>
</ul>
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>并查集</li><li>数组</li><li>矩阵</li></div></div>\n<div><li>👍 83</li><li>👎 0</li></div>