@@ -223,7 +223,121 @@ public:
223
223
## 其他语言版本
224
224
225
225
### Java
226
-
226
+ DFS
227
+ ```java
228
+ //这里的实现为主函数处理每个岛屿的第一块陆地 方式
229
+ //所以是主函数直接置count为1,剩余的交给dfs来做。
230
+ import java.util.*;
231
+ public class Main{
232
+ static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1, 0}};//四个方向
233
+ static int count = 0;
234
+ public static void dfs(boolean[][] visited, int x, int y, int[][] grid){
235
+ for(int i = 0; i < 4; i++){
236
+ int nextX = x + dir[i][0];
237
+ int nextY = y + dir[i][1];
238
+ if(nextX < 0 || nextY < 0 || nextY >= grid[0].length || nextX >= grid.length){
239
+ continue;
240
+ }
241
+ if(!visited[nextX][nextY] && grid[nextX][nextY] == 1){
242
+ count++;
243
+ visited[nextX][nextY] = true;
244
+ dfs(visited, nextX, nextY, grid);
245
+ }
246
+ }
247
+ }
248
+ public static void main(String[] args){
249
+ Scanner in = new Scanner(System.in);
250
+ int n = in.nextInt();
251
+ int m = in.nextInt();
252
+ int[][] grid = new int[n][m];
253
+ for(int i = 0; i < n; i++){
254
+ for(int j = 0; j < m; j++){
255
+ grid[i][j] = in.nextInt();
256
+ }
257
+ }
258
+
259
+ int result = 0;
260
+ boolean[][] visited = new boolean[n][m];
261
+ for(int i = 0; i < n; i++){
262
+ for(int j = 0; j < m; j++){
263
+ if(!visited[i][j] && grid[i][j] == 1){
264
+ visited[i][j] = true;
265
+ count = 1;
266
+ dfs(visited, i, j, grid);
267
+ //dfs遍历完了一座岛屿,就比较count和result,保留最大的
268
+ result = Math.max(result, count);
269
+ }
270
+ }
271
+ }
272
+ System.out.println(result);
273
+ }
274
+ }
275
+ ```
276
+ BFS
277
+ ``` java
278
+ import java.util.* ;
279
+ public class Main {
280
+ static int [][] dir = {{0 ,- 1 }, {1 ,0 }, {0 ,1 }, {- 1 , 0 }};// 下右上左的顺序
281
+ static int count = 0 ;
282
+ public static void bfs (boolean [][] visited , int x , int y , int [][] grid ){
283
+ Queue<pair> queue = new LinkedList<pair> ();
284
+ queue. add(new pair(x,y));
285
+ count = 1 ; // 该岛屿的第一块陆地被visit了
286
+
287
+ // 对这个岛屿的所有都入队,除非上下左右都没有未访问的陆地
288
+ while (! queue. isEmpty()){
289
+ int curX = queue. peek(). x;
290
+ int curY = queue. poll(). y;
291
+ // 对每块陆地都进行上下左右的入队和计算(遍历),自然就是按广度优先了
292
+ for (int i = 0 ; i < 4 ; i++ ){
293
+ int nextX = curX + dir[i][0 ];
294
+ int nextY = curY + dir[i][1 ];
295
+ if (nextX < 0 || nextY < 0 || nextX >= grid. length || nextY >= grid[0 ]. length){
296
+ continue ;
297
+ }
298
+ if (! visited[nextX][nextY] && grid[nextX][nextY] == 1 ){
299
+ count++ ;
300
+ queue. add(new pair(nextX, nextY));
301
+ visited[nextX][nextY] = true ;
302
+ }
303
+ }
304
+ }
305
+ }
306
+
307
+ static class pair {
308
+ int x;
309
+ int y;
310
+ pair (int x , int y ){
311
+ this . x = x;
312
+ this . y = y;
313
+ }
314
+ }
315
+
316
+ public static void main (String [] args ){
317
+ Scanner in = new Scanner (System . in);
318
+ int n = in. nextInt();
319
+ int m = in. nextInt();
320
+ int [][] grid = new int [n][m];
321
+ for (int i = 0 ; i < n; i++ ){
322
+ for (int j = 0 ; j < m; j++ ){
323
+ grid[i][j] = in. nextInt();
324
+ }
325
+ }
326
+ int result = 0 ;
327
+ boolean [][] visited = new boolean [n][m];
328
+ for (int i = 0 ; i < n; i++ ){
329
+ for (int j = 0 ; j < m; j++ ){
330
+ if (! visited[i][j] && grid[i][j] == 1 ){
331
+ visited[i][j] = true ;
332
+ bfs(visited, i, j, grid);
333
+ result = Math . max(result, count);
334
+ }
335
+ }
336
+ }
337
+ System . out. println(result);
338
+ }
339
+ }
340
+ ```
227
341
### Python
228
342
229
343
DFS
0 commit comments