1254:统计封闭岛屿的数目
This commit is contained in:
parent
5346b60d90
commit
597448a9fc
117
src/main/java/leetcode/editor/cn/NumberOfClosedIslands.java
Normal file
117
src/main/java/leetcode/editor/cn/NumberOfClosedIslands.java
Normal 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)
|
||||
|
||||
}
|
48
src/main/java/leetcode/editor/cn/NumberOfClosedIslands.md
Normal file
48
src/main/java/leetcode/editor/cn/NumberOfClosedIslands.md
Normal file
@ -0,0 +1,48 @@
|
||||
<p>有一个二维矩阵 <code>grid</code> ,每个位置要么是陆地(记号为 <code>0</code> )要么是水域(记号为 <code>1</code> )。</p>
|
||||
|
||||
<p>我们从一块陆地出发,每次可以往上下左右 4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座「<strong>岛屿</strong>」。</p>
|
||||
|
||||
<p>如果一座岛屿 <strong>完全</strong> 由水域包围,即陆地边缘上下左右所有相邻区域都是水域,那么我们将其称为 「<strong>封闭岛屿</strong>」。</p>
|
||||
|
||||
<p>请返回封闭岛屿的数目。</p>
|
||||
|
||||
<p> </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],
|
||||
[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]]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= grid.length, grid[0].length <= 100</code></li>
|
||||
<li><code>0 <= grid[i][j] <=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>
|
Loading…
Reference in New Issue
Block a user