# 200 Number of Islands

## 200. [Number of Islands](https://leetcode.com/problems/number-of-islands/description/)

## 1. Question

Given a 2d grid map of`'1'`s (land) and`'0'`s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

**Example 1:**

```
11110
11010
11000
00000
```

Answer: 1

**Example 2:**

```
11000
11000
00100
00011
```

Answer: 3

## 2. Implementation

**(1) BFS**

```java
class Solution {
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }

        int[][] directions = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
        int m = grid.length, n = grid[0].length;
        int count = 0;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    ++count;
                    searchByBFS(grid, i, j, m, n, directions);
                }
            }
        }
        return count;
    }

    public void searchByBFS(char[][] grid, int row, int col, int m, int n, int[][] directions) {
        grid[row][col] = '0';

        Queue<Integer> queue = new LinkedList<>();
        queue.add(row * n + col);

        while (!queue.isEmpty()) {
            int curPoint = queue.remove();
            int curRow = curPoint / n;
            int curCol = curPoint % n;

            for (int[] direction : directions) {
                int nextRow = curRow + direction[0];
                int nextCol = curCol + direction[1];

                if (isValid(grid, nextRow, nextCol)) {
                    grid[nextRow][nextCol] = '0';
                    queue.add(nextRow * n + nextCol);
                }
            }
        }
    }

    public boolean isValid(char[][] grid, int row, int col) {
        return row >= 0 && row < grid.length && col >= 0 && col < grid[0].length && grid[row][col] == '1';
    }
}
```

**(2) DFS**

```java
class Solution {
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }

        int m = grid.length, n = grid[0].length;
        int count = 0;

        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    ++count;
                    searchIslandByDFS(i, j, grid, directions);
                }
            }
        }
        return count;
    }

    public void searchIslandByDFS(int row, int col, char[][] grid, int[][] directions) {
        grid[row][col] = '0';

        for (int[] direction : directions) {
            int nextRow = row + direction[0];
            int nextCol = col + direction[1];

            if (isValid(nextRow, nextCol, grid)) {
                searchIslandByDFS(nextRow, nextCol, grid, directions);
            }
        }
    }

    public boolean isValid(int row, int col, char[][] grid) {
        return row >= 0 && row < grid.length && col >= 0 && col < grid[0].length && grid[row][col] == '1';
    } 
}
```

## 3. Time & Space Complexity

BFS: 时间复杂度O(mn), 空间复杂度:O(mn)

DFS: 时间复杂度O(mn), 空间复杂度:O(mn)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/leetcode/graph/200-number-of-islands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
