mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
feat: 0100.岛屿最大面积 Java实现
This commit is contained in:
@ -224,6 +224,127 @@ public:
|
|||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
|
```java
|
||||||
|
import java.util.*;
|
||||||
|
import java.math.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DFS版
|
||||||
|
*/
|
||||||
|
public class Main{
|
||||||
|
|
||||||
|
static final int[][] dir={{0,1},{1,0},{0,-1},{-1,0}};
|
||||||
|
static int result=0;
|
||||||
|
static int count=0;
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
int m = scanner.nextInt();
|
||||||
|
int[][] map = new int[n][m];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
map[i][j]=scanner.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boolean[][] visited = new boolean[n][m];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if(!visited[i][j]&&map[i][j]==1){
|
||||||
|
count=0;
|
||||||
|
dfs(map,visited,i,j);
|
||||||
|
result= Math.max(count, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dfs(int[][] map,boolean[][] visited,int x,int y){
|
||||||
|
count++;
|
||||||
|
visited[x][y]=true;
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
int nextX=x+dir[i][0];
|
||||||
|
int nextY=y+dir[i][1];
|
||||||
|
//水或者已经访问过的跳过
|
||||||
|
if(nextX<0||nextY<0
|
||||||
|
||nextX>=map.length||nextY>=map[0].length
|
||||||
|
||visited[nextX][nextY]||map[nextX][nextY]==0)continue;
|
||||||
|
|
||||||
|
dfs(map,visited,nextX,nextY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
import java.util.*;
|
||||||
|
import java.math.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BFS版
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
static class Node {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
public Node(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static final int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
|
||||||
|
static int result = 0;
|
||||||
|
static int count = 0;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
int m = scanner.nextInt();
|
||||||
|
int[][] map = new int[n][m];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
map[i][j] = scanner.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boolean[][] visited = new boolean[n][m];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if (!visited[i][j] && map[i][j] == 1) {
|
||||||
|
count = 0;
|
||||||
|
bfs(map, visited, i, j);
|
||||||
|
result = Math.max(count, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void bfs(int[][] map, boolean[][] visited, int x, int y) {
|
||||||
|
Queue<Node> q = new LinkedList<>();
|
||||||
|
q.add(new Node(x, y));
|
||||||
|
visited[x][y] = true;
|
||||||
|
count++;
|
||||||
|
while (!q.isEmpty()) {
|
||||||
|
Node node = q.remove();
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
int nextX = node.x + dir[i][0];
|
||||||
|
int nextY = node.y + dir[i][1];
|
||||||
|
if (nextX < 0 || nextY < 0 || nextX >= map.length || nextY >= map[0].length || visited[nextX][nextY] || map[nextX][nextY] == 0)
|
||||||
|
continue;
|
||||||
|
q.add(new Node(nextX, nextY));
|
||||||
|
visited[nextX][nextY] = true;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
DFS
|
DFS
|
||||||
|
Reference in New Issue
Block a user