99.岛屿数量 深搜/广搜
题目链接:https://kamacoder.com/problempage.php?pid=1171
文档讲解:深搜、广搜
思路
遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。
代码
import java.util.*;
class Main {
static int[][] grid;
static boolean[][] visited;
static int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
grid = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
grid[i][j] = in.nextInt();
}
}
visited = new boolean[n][m];
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && grid[i][j] == 1) {
res++;
visited[i][j] = true;
// dfs(i, j);
bfs(i, j);
}
}
}
System.out.println(res);
}
public static void bfs(int x, int y) {
Deque<int[]> deque = new LinkedList<>();
deque.push(new int[]{x, y});
visited[x][y] = true;
while (!deque.isEmpty()) {
int[] cur = deque.pop();
int curX = cur[0];
int curY = cur[1];
for (int i = 0; i < 4; i++) {
int nextX = curX + dir[i][0];
int nextY = curY + dir[i][1];
if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
deque.push(new int[]{nextX, nextY});
visited[nextX][nextY] = true; // 所以只要加入队列,立即标记该节点走过。否则会超时
}
}
}
}
public static void dfs(int x, int y) {
for (int i = 0; i < 4; i++) {
int nextX = x + dir[i][0];
int nextY = y + dir[i][1];
if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
visited[nextX][nextY] = true;
dfs(nextX, nextY);
}
}
}
}
100.岛屿的最大面积
题目链接:https://kamacoder.com/problempage.php?pid=1172
文档讲解:https://programmercarl.com/kamacoder/0100.%E5%B2%9B%E5%B1%BF%E7%9A%84%E6%9C%80%E5%A4%A7%E9…
思路
搜索每个岛屿上“1”的数量,然后取一个最大的。
代码
import java.util.*;
class Main{
static int[][] grid;
static boolean[][] visited;
static int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
static int count;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
grid = new int[n][m];
visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
grid[i][j] = in.nextInt();
}
}
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && grid[i][j] == 1) {
count = 1;
visited[i][j] = true;
//dfs(i, j);
bfs(i, j);
res = Math.max(res, count);
}
}
}
System.out.println(res);
}
public static void bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{x, y});
while (!queue.isEmpty()) {
int[] cur = queue.poll();
int curX = cur[0], curY = cur[1];
for (int i = 0; i < 4; i++) {
int nextX = curX + dir[i][0], nextY = curY + dir[i][1];
if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
count++;
queue.offer(new int[]{nextX, nextY});
visited[nextX][nextY] = true;
}
}
}
}
public static void dfs(int x, int y) {
for (int i = 0; i < 4; i++) {
int nextX = x + dir[i][0];
int nextY = y + dir[i][1];
if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) continue;
if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
count++;
visited[nextX][nextY] = true;
dfs(nextX, nextY);
}
}
}
}