> For the complete documentation index, see [llms.txt](https://phitron.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://phitron.gitbook.io/algorithm/bfs-dfs-problem-solving/_-number-of-islands-leetcode.md).

# মডিউল ৯\_৪ঃ Number of Islands \[Leetcode]

একটি m x n 2D বাইনারি  গ্রিড দেওয়া হয়েছে যা '1' (ভূমি) এবং '0' (জল) রিপ্রেজেন্ট  করা হয়েছে, আপনার থেকে বের করতে হবে গ্রিডে কয়টি দ্বীপ রয়েছে।&#x20;

ভূমিগুলো অনুভূমিকভাবে বা উল্লম্বভাবে ভাবে একত্রিত হয়ে আছে। একটি দ্বীপ জল দ্বারা বেষ্টিত থাকে। <br>

<figure><img src="/files/GgAQgPg6y5nWMtYV0RDQ" alt=""><figcaption></figcaption></figure>

উপরের চিত্রের গ্রিডটিতে ৩ টি দ্বীপ পাওয়া গিয়েছে। \
গ্রিডটিতে কয়টি দ্বীপ আছে সেইটি আপনার থেকে রিটার্ন করতে হবে।&#x20;

## সমাধান

এই প্রব্লেমটিতে grid\[i]\[j] ==1 পেলে এবং সেইটি আনভিজিটেড হলে সেইটিকে dfs দিয়ে traversal করবো, এইভাবে যতবার dfs কল করেছি ততবার এক একটি দ্বীপকে traverse করেছি এবং দ্বীপকে count করেছি।&#x20;

```cpp
n = grid.size();
m = grid[0].size();
memset(vis, false, sizeof(vis));
int ans = 0;
```

vis array তে false assign করেছি। ans = 0 নিয়েছি, n এ grid এর number of row এবং m এ grid এর number of col রেখেছি।&#x20;

```cpp
for (int i = 0; i < n; i++)
{
    for (int j = 0; j < m; j++)
    {
        if (!vis[i][j] && grid[i][j] == '1')
        {
            dfs(i, j, grid);
            ans++;
        }
    }
}
```

এখন nested লুপ চালিয়েছি, সেই grid আনভিজিটেড কিনা এবং  grid ==1 কিনা চেক করে dfs কে কল করেছি এবং ans++ করেছি।&#x20;

```cpp
void dfs(int si, int sj, vector<vector<char>> &grid)
{
    vis[si][sj] = true;
    for (int i = 0; i < 4; i++)
    {
        int ci = si + d[i].first;
        int cj = sj + d[i].second;
        if (valid(ci, cj) && !vis[ci][cj] && grid[ci][cj] == '1')
        {
            dfs(ci, cj, grid);
        }
    }
}
```

dfs function এ প্রথমে si এবং sj কে ভিজিটেড করেছি,  এরপর grid এর উপরে , নিচে , ডানে , বামে যাওয়ার জন্য লুপ চালিয়েছি। \
যদি ci, cj ভ্যালিড এবং আনভিজিটেড এবং grid\[ci]\[cj]==1 হয় তাহলে ci,cj কে দিয়ে dfs function কে recursive কল করবো।&#x20;

## সম্পূর্ণ কোড&#x20;

```cpp
#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    int n, m;
    bool vis[305][305];
    vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
    bool valid(int ci, int cj)
    {
        if (ci >= 0 && ci < n && cj >= 0 && cj < m)
            return true;
        else
            return false;
    }
    void dfs(int si, int sj, vector<vector<char>> &grid)
    {
        vis[si][sj] = true;
        for (int i = 0; i < 4; i++)
        {
            int ci = si + d[i].first;
            int cj = sj + d[i].second;
            if (valid(ci, cj) && !vis[ci][cj] && grid[ci][cj] == '1')
            {
                dfs(ci, cj, grid);
            }
        }
    }
    int numIslands(vector<vector<char>> &grid)
    {
        n = grid.size();
        m = grid[0].size();
        memset(vis, false, sizeof(vis));
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (!vis[i][j] && grid[i][j] == '1')
                {
                    dfs(i, j, grid);
                    ans++;
                }
            }
        }
        return ans;
    }
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://phitron.gitbook.io/algorithm/bfs-dfs-problem-solving/_-number-of-islands-leetcode.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
