@@ -222,7 +222,128 @@ public:
222222
223223## 其他语言版本
224224
225- ### Java
225+ ### Java
226+
227+ ```java
228+ import java.util.*;
229+ import java.math.*;
230+
231+ /**
232+ * DFS版
233+ */
234+ public class Main{
235+
236+ static final int[][] dir={{0,1},{1,0},{0,-1},{-1,0}};
237+ static int result=0;
238+ static int count=0;
239+
240+ public static void main(String[] args){
241+ Scanner scanner = new Scanner(System.in);
242+ int n = scanner.nextInt();
243+ int m = scanner.nextInt();
244+ int[][] map = new int[n][m];
245+ for (int i = 0; i < n; i++) {
246+ for (int j = 0; j < m; j++) {
247+ map[i][j]=scanner.nextInt();
248+ }
249+ }
250+ boolean[][] visited = new boolean[n][m];
251+ for (int i = 0; i < n; i++) {
252+ for (int j = 0; j < m; j++) {
253+ if(!visited[i][j]&&map[i][j]==1){
254+ count=0;
255+ dfs(map,visited,i,j);
256+ result= Math.max(count, result);
257+ }
258+ }
259+ }
260+ System.out.println(result);
261+ }
262+
263+ static void dfs(int[][] map,boolean[][] visited,int x,int y){
264+ count++;
265+ visited[x][y]=true;
266+ for (int i = 0; i < 4; i++) {
267+ int nextX=x+dir[i][0];
268+ int nextY=y+dir[i][1];
269+ //水或者已经访问过的跳过
270+ if(nextX<0||nextY<0
271+ ||nextX>=map.length||nextY>=map[0].length
272+ ||visited[nextX][nextY]||map[nextX][nextY]==0)continue;
273+
274+ dfs(map,visited,nextX,nextY);
275+ }
276+ }
277+ }
278+ ```
279+
280+ ``` java
281+ import java.util.* ;
282+ import java.math.* ;
283+
284+ /**
285+ * BFS版
286+ */
287+ public class Main {
288+ static class Node {
289+ int x;
290+ int y;
291+
292+ public Node (int x , int y ) {
293+ this . x = x;
294+ this . y = y;
295+ }
296+ }
297+
298+ static final int [][] dir = {{0 , 1 }, {1 , 0 }, {0 , - 1 }, {- 1 , 0 }};
299+ static int result = 0 ;
300+ static int count = 0 ;
301+
302+ public static void main (String [] args ) {
303+ Scanner scanner = new Scanner (System . in);
304+ int n = scanner. nextInt();
305+ int m = scanner. nextInt();
306+ int [][] map = new int [n][m];
307+ for (int i = 0 ; i < n; i++ ) {
308+ for (int j = 0 ; j < m; j++ ) {
309+ map[i][j] = scanner. nextInt();
310+ }
311+ }
312+ boolean [][] visited = new boolean [n][m];
313+ for (int i = 0 ; i < n; i++ ) {
314+ for (int j = 0 ; j < m; j++ ) {
315+ if (! visited[i][j] && map[i][j] == 1 ) {
316+ count = 0 ;
317+ bfs(map, visited, i, j);
318+ result = Math . max(count, result);
319+ }
320+ }
321+ }
322+ System . out. println(result);
323+ }
324+
325+ static void bfs (int [][] map , boolean [][] visited , int x , int y ) {
326+ Queue<Node > q = new LinkedList<> ();
327+ q. add(new Node (x, y));
328+ visited[x][y] = true ;
329+ count++ ;
330+ while (! q. isEmpty()) {
331+ Node node = q. remove();
332+ for (int i = 0 ; i < 4 ; i++ ) {
333+ int nextX = node. x + dir[i][0 ];
334+ int nextY = node. y + dir[i][1 ];
335+ if (nextX < 0 || nextY < 0 || nextX >= map. length || nextY >= map[0 ]. length || visited[nextX][nextY] || map[nextX][nextY] == 0 )
336+ continue ;
337+ q. add(new Node (nextX, nextY));
338+ visited[nextX][nextY] = true ;
339+ count++ ;
340+ }
341+ }
342+ }
343+ }
344+
345+ ```
346+
226347
227348### Python
228349
0 commit comments