695. 岛屿的最大面积
给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。
找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)
示例 1:
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。
示例 2:
[[0,0,0,0,0,0,0,0]]
对于上面这个给定的矩阵, 返回 0。
注意: 给定的矩阵grid 的长度和宽度都不超过 50。
DFS
思路分析:基于回溯法思路
1、max 记录最大岛屿面积
2、visited[][] 记录当前坐标是否已被访问
3、当遍历一个未被访问过的 1 时,向上下左右进行遍历,每遍历一个 1岛屿面积+1
class Solution {
int[][] move = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private boolean isValid(int[][] grid, boolean[][] visited, int x, int y){
if(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || visited[x][y] || grid[x][y] != 1){
return false;
}
return true;
}
private int dfs(int[][] grid, boolean[][] visited, int x, int y, int count){
if(!isValid(grid, visited, x, y)){
return count;
}
visited[x][y] = true;
for(int i = 0; i < move.length; ++i){
count = dfs(grid, visited, x + move[i][0], y + move[i][1], count);
}
return count + 1;
}
public int maxAreaOfIsland(int[][] grid) {
int max = 0, row = grid.length, col = grid[0].length;
boolean[][] visited = new boolean[row][col];
for(int i = 0; i < row; ++i){
for(int j = 0; j < col; ++j){
if(grid[i][j] == 1){
int count = dfs(grid, visited, i, j, 0);
max = max > count ? max : count;
}
}
}
return max;
}
}
200. 岛屿数量
给定一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
示例 1:
输入:
11110
11010
11000
00000
输出: 1
示例 2:
输入:
11000
11000
00100
00011
输出: 3
DFS
跟上一题思路一致,上题是求最大面积,这题是求个数。
class Solution {
int[][] move = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private int dfs(char[][] grid, boolean[][] visited, int x, int y, int count){
if(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] != '1' || visited[x][y])
return count;
visited[x][y] = true;
for(int i = 0; i < move.length; ++i){
count = dfs(grid, visited, x + move[i][0], y + move[i][1], count);
}
return count+1;
}
public int numIslands(char[][] grid) {
if(grid.length == 0)
return 0;
int total = 0, row = grid.length, col = grid[0].length;
boolean[][] visited = new boolean[row][col];
int count = 0;
for(int i = 0; i < row; ++i){
for(int j = 0; j < col; ++j){
if(grid[i][j] == '1'){
count = dfs(grid, visited, i, j, 0);
if(count >= 1){
total++;
}
}
}
}
return total;
}
}
BFS
线性扫描整个二维网格,如果一个结点包含 1,则以其为根结点启动广度优先搜索。将其放入队列中,并将值设为 0 以标记访问过该结点。迭代地搜索队列中的每个结点,直到队列为空。
class Solution {
public int numIslands(char[][] grid) {
if(grid == null || grid.length == 0){
return 0;
}
int total = 0, row = grid.length, col = grid[0].length;
for(int i = 0; i < row; ++i){
for(int j = 0; j < col; ++j){
if(grid[i][j] == '1'){
++total;
grid[i][j] = '0';
Queue<Integer> queue = new LinkedList<>();
queue.add(i * col + j);
while(!queue.isEmpty()){
int id = queue.remove();
int r = id / col;
int c = id % col;
if(r - 1 >= 0 && grid[r - 1][c] == '1'){
queue.add((r - 1) * col + c);
grid[r - 1][c] = '0';
}
if(r + 1 < row && grid[r + 1][c] == '1'){
queue.add((r + 1) * col + c);
grid[r + 1][c] = '0';
}
if(c - 1 >= 0 && grid[r][c - 1] == '1'){
queue.add(r * col + c - 1);
grid[r][c - 1] = '0';
}
if(c + 1 < col && grid[r][c + 1] == '1'){
queue.add(r * col + c + 1);
grid[r][c + 1] = '0';
}
}
}
}
}
return total;
}
}
并查集
class Solution {
class UnionFind{
int count;
int[] parent;
public UnionFind(char[][] grid){
count = 0;
int m = grid.length;
int n = grid[0].length;
parent = new int[m * n];
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(grid[i][j] == '1'){
parent[i * n + j] = i * n + j;
++count;
}
}
}
}
public int find(int i){
if(parent[i] != i) parent[i] = find(parent[i]);
return parent[i];
}
public void union(int x, int y){
int rootx = find(x);
int rooty = find(y);
if(rootx != rooty){
parent[rootx] = rooty;
--count;
}
}
public int getCount(){
return count;
}
}
public int numIslands(char[][] grid) {
if(grid == null || grid.length == 0){
return 0;
}
int total = 0, row = grid.length, col = grid[0].length;
UnionFind uf = new UnionFind(grid);
for(int i = 0; i < row; ++i){
for(int j = 0; j < col; ++j){
if(grid[i][j] == '1'){
grid[i][j] = '0';
int old = i * col + j;
if(i - 1 >= 0 && grid[i - 1][j] == '1'){
uf.union(old, (i - 1) * col + j);
}
if(i + 1 < row && grid[i + 1][j] == '1'){
uf.union(old, (i + 1) * col + j);
}
if(j - 1 >= 0 && grid[i][j - 1] == '1'){
uf.union(old, i * col + j - 1);
}
if(j + 1 < col && grid[i][j + 1] == '1'){
uf.union(old, i * col + j + 1);
}
}
}
}
return uf.getCount();
}
}