@@ -222,121 +222,127 @@ public:
222222
223223## 其他语言版本
224224
225- ### Java
226- DFS
225+ ### Java
226+
227227```java
228- //这里的实现为主函数处理每个岛屿的第一块陆地 方式
229- //所以是主函数直接置count为1,剩余的交给dfs来做。
230228import java.util.*;
229+ import java.math.*;
230+
231+ /**
232+ * DFS版
233+ */
231234public 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- }
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+
248240 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();
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();
256248 }
257249 }
258-
259- int result = 0;
260250 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);
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);
269257 }
270258 }
271259 }
272260 System.out.println(result);
273261 }
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+ }
274277}
275278```
276- BFS
279+
277280``` java
278281import 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 {
282+ import java.math.* ;
283+
284+ /**
285+ * BFS版
286+ */
287+ public class Main {
288+ static class Node {
308289 int x;
309290 int y;
310- pair (int x , int y ){
291+
292+ public Node (int x , int y ) {
311293 this . x = x;
312294 this . y = y;
313295 }
314296 }
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();
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();
324310 }
325311 }
326- int result = 0 ;
327312 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);
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+
334320 }
335321 }
336322 }
337323 System . out. println(result);
338324 }
325+
326+ static void bfs (int [][] map , boolean [][] visited , int x , int y ) {
327+ Queue<Node > q = new LinkedList<> ();
328+ q. add(new Node (x, y));
329+ visited[x][y] = true ;
330+ count++ ;
331+ while (! q. isEmpty()) {
332+ Node node = q. remove();
333+ for (int i = 0 ; i < 4 ; i++ ) {
334+ int nextX = node. x + dir[i][0 ];
335+ int nextY = node. y + dir[i][1 ];
336+ if (nextX < 0 || nextY < 0 || nextX >= map. length || nextY >= map[0 ]. length || visited[nextX][nextY] || map[nextX][nextY] == 0 )
337+ continue ;
338+ q. add(new Node (nextX, nextY));
339+ visited[nextX][nextY] = true ;
340+ count++ ;
341+ }
342+ }
343+ }
339344}
345+
340346```
341347### Python
342348
0 commit comments