1.png

    1. class Solution {
    2. public int maxAreaOfIsland(int[][] grid) {
    3. int ans = 0;
    4. for (int i = 0; i != grid.length; ++i) {
    5. for (int j = 0; j != grid[0].length; ++j) {
    6. int cur = 0;
    7. Queue<Integer> queuei = new LinkedList<Integer>();
    8. Queue<Integer> queuej = new LinkedList<Integer>();
    9. queuei.offer(i);
    10. queuej.offer(j);
    11. while (!queuei.isEmpty()) {
    12. int cur_i = queuei.poll(), cur_j = queuej.poll();
    13. if (cur_i < 0 || cur_j < 0 || cur_i == grid.length || cur_j == grid[0].length || grid[cur_i][cur_j] != 1) {
    14. continue;
    15. }
    16. ++cur;
    17. grid[cur_i][cur_j] = 0;
    18. int[] di = {0, 0, 1, -1};
    19. int[] dj = {1, -1, 0, 0};
    20. for (int index = 0; index != 4; ++index) {
    21. int next_i = cur_i + di[index], next_j = cur_j + dj[index];
    22. queuei.offer(next_i);
    23. queuej.offer(next_j);
    24. }
    25. }
    26. ans = Math.max(ans, cur);
    27. }
    28. }
    29. return ans;
    30. }
    31. }

    广度