岛屿数量_找出岛屿的数量

岛屿数量

Problem Statement:

问题陈述:

A group of connected 1's forms an island. There is a matrix of N×M. You have to find out the numbers of item.

一组相连的1组成一个岛 。 有一个N×M的矩阵。 您必须找出项目编号。

Example:

例:

If there is a matrix:

如果存在矩阵:

find the number of islands

Here three groups ones are possible. One is red one is yellow and one is blue.
Output: 3

在这里三组是可能的。 一是红色,一是黄色,一是蓝色。
输出3

Algorithm:

算法:

To solve this problem we follow this approach:

为了解决这个问题,我们采用以下方法:

  1. We take a stack and whenever we find the first 1 we push the position into the stack and make that position visited.

    我们取一个堆栈,每当找到第一个1时,我们就将该位置推入堆栈,并使该位置被访问。

  2. Take the first element from the stack and pop from the stack.

    从堆栈中取出第一个元素,然后从堆栈中弹出。

  3. Check it's four neighbors. If out of them if anyone is 1 then we also push the position into the stack.

    检查它是四个邻居。 如果其中任何一个为1,则我们还将位置推入堆栈。

  4. Repeat the process until the queue is empty.

    重复该过程,直到队列为空。

  5. Count the numbers when the queue is empty and its give the island count.

    计算队列为空时的数字,并给出孤岛计数。

C ++实现查找孤岛数 (C++ Implementation to find the number of islands)

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

int x[] = { -1, 1, 0, 0 };
int y[] = { 0, 0, -1, 1 };
//check the validity of the position
//with respect to the matrix.
bool isvalid(int x_axis, int y_axis, int n)
{
    return (x_axis >= 0 && x_axis < n && y_axis >= 0 && y_axis < n);
}
//function to count the no. of Island
int Island_count(int* arr, int n)
{
    int count = 0;
    stack<pair<int, int> > s;
    set<pair<int, int> > st;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (*(arr + i * n + j) == 1 && st.find(make_pair(i, j)) == st.end()) {
                //push the position with value 1 into the stack
                s.push(make_pair(i, j));
                //make the position visited
                st.insert(make_pair(i, j));
                while (!s.empty()) {
                    //take the top element of the stack
                    pair<int, int> p = s.top();
                    s.pop();
                    //check it's neighbours
                    for (int i = 0; i < 4; i++) {
                        int x_axis = p.first + x[i];
                        int y_axis = p.second + y[i];
                        //if the position is valid and it is not visited
                        //then push that position into stack
                        if (isvalid(x_axis, y_axis, n) && st.find(make_pair(x_axis, y_axis)) == st.end() && *(arr + x_axis * n + y_axis) == 1) {
                            s.push(make_pair(x_axis, y_axis));
                            st.insert(make_pair(x_axis, y_axis));
                        }
                    }
                }
                //every time when stack is empty means
                //we visited all the group of 1's
                count++;
            }
        }
    }
    return count;
}

int main()
{
    int size = 4;
    int arr[size][size] = { { 1, 1, 0, 0 }, { 1, 0, 1, 1 }, { 1, 1, 0, 1 }, { 0, 0, 1, 0 } };
    cout << "No. Of Island is : ";
    cout << Island_count(&arr[0][0], size) << endl;
    return 0;
}

Output

输出量

The No. of Island is : 3


翻译自: https://www.includehelp.com/icp/find-the-number-of-islands.aspx

岛屿数量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
矩阵岛屿是一个常见的计算机算法问题,通常用于解决图论和搜索相关的问题。在Python中,我们可以使用深度优先搜索(DFS)算法来解决矩阵岛屿问题。 首先,我们需要将矩阵表示为一个二维数组。每个元素可以通过0或1来表示,其中0表示水域,1表示陆地。我们需要遍历整个二维数组,当遇到1(表示陆地)时,就进行DFS搜索,找到与当前陆地相邻的所有陆地,将其标记为已访问,并继续搜索相邻的陆地。通过这样的遍历和搜索,我们可以计算出矩阵中的岛屿数量。 下面是一个简单的Python实现示例: ```python def dfs(grid, i, j): # 判断当前坐标是否越界或者已经访问过 if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != 1: return # 标记当前坐标为已访问 grid[i][j] = '#' # 搜索上下左右四个方向 dfs(grid, i - 1, j) dfs(grid, i + 1, j) dfs(grid, i, j - 1) dfs(grid, i, j + 1) def numIslands(grid): count = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1: # 发现一个新的岛屿,进行DFS搜索 dfs(grid, i, j) count += 1 return count # 示例输入 grid = [ [1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 1] ] # 调用函数计算岛屿数量 islands = numIslands(grid) print("岛屿数量为:", islands) ``` 运行上述代码,输出结果为:岛屿数量为:3。表示给定的矩阵中有3个岛屿。这就是通过深度优先搜索算法来解决矩阵岛屿问题的一个简单实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值