99.岛屿数量 深搜
每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
本题思路,是用遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。
在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。
那么如何把节点陆地所能遍历到的陆地都标记上呢,就可以使用 DFS,BFS或者并查集。
java
import java.util.*;
public class Main {
static int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
static int ans = 0;
static boolean[][] visited;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] graph = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
graph[i][j] = sc.nextInt();
}
}
visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(!visited[i][j] && graph[i][j] == 1){
bfs(i,j,graph,n,m);
ans++;
}
}
}
System.out.println(ans);
}
private static void bfs(int row, int col, int[][] graph, int n, int m) {
if (visited[row][col]){
return;
}
visited[row][col] = true;
for (int[] dir : dirs) {
int nextR = dir[0] + row;
int nextC = dir[1] + col;
if (nextR >= 0 && nextR < n && nextC >= 0 && nextC < m){
if (graph[nextR][nextC] == 1){
bfs(nextR,nextC,graph,n,m);
}
}
}
}
}
广搜
java
class Solution {
public int numIslands(char[][] grid) {
int count = 0;
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
if(grid[i][j] == '1'){
bfs(grid, i, j);
count++;
}
}
}
return count;
}
private void bfs(char[][] grid, int i, int j){
Queue<int[]> list = new LinkedList<>();
list.add(new int[] { i, j });
while(!list.isEmpty()){
int[] cur = list.remove();
i = cur[0]; j = cur[1];
if(0 <= i && i < grid.length && 0 <= j && j < grid[0].length && grid[i][j] == '1') {
grid[i][j] = '0';
list.add(new int[] { i + 1, j });
list.add(new int[] { i - 1, j });
list.add(new int[] { i, j + 1 });
list.add(new int[] { i, j - 1 });
}
}
}
}
100.岛屿的最大面积
java
public class Main {
static int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
static boolean[][] visited;
static int ans = Integer.MIN_VALUE;
static int temp;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] graph = new int[n][m];
visited = new boolean[n][m];
// 初始化
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
graph[i][j] = sc.nextInt();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && graph[i][j] == 1){
temp = 0;
bfs(i, j, n, m, graph);
ans = Math.max(ans, temp);
}
}
}
System.out.printf("%d", ans == Integer.MIN_VALUE ? 0:ans);
}
private static void dfs(int row, int col, int n, int m, int[][] graph) {
if (visited[row][col]){
return;
}
visited[row][col] = true;
temp++;
for (int[] dir : dirs) {
int nextR = row + dir[0];
int nextC = col + dir[1];
if(nextR >= 0 && nextR < n && nextC >= 0 && nextC < m && graph[nextR][nextC] == 1){
bfs(nextR,nextC,n,m,graph);
}
}
}
}
java
class Solution {
public int maxAreaOfIsland(int[][] grid) {
int res = 0;
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++){
if(grid[i][j] == 1){
res = Math.max(res, bfs(grid, i, j));
}
}
}
return res;
}
public int bfs(int[][] grid, int i, int j){
int[] dx = {1, -1, 0, 0};
int[] dy = {0, 0, 1, -1};
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{i, j});
grid[i][j] = 0;
int area = 1;
while(!queue.isEmpty()){
int[] x = queue.poll();
for(int index = 0; index < 4; index++){
int nx = x[0] + dx[index], ny = x[1] + dy[index];
if(nx>=0 && nx < grid.length && ny >= 0 && ny < grid[0].length && grid[nx][ny] == 1){
grid[nx][ny] = 0;
area += 1;
queue.offer(new int[]{nx, ny});
}
}
}
return area;
}
}