代码随想录: 图论| 岛屿数量

题目链接:99. 岛屿数量

题目描述:

给定一个由 1(陆地)和 0(水)组成的矩阵,你需要计算岛屿的数量。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。

输入描述:

第一行包含两个整数 N, M,表示矩阵的行数和列数。

后续 N 行,每行包含 M 个数字,数字为 1 或者 0。

输出描述:

输出一个整数,表示岛屿的数量。如果不存在岛屿,则输出 0。

输入示例:

4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1

输出示例:

思路

本题DFS和BFS都可以做 首先使用DFS方法解题

1.本题用朴素方式存储陆地和水组成的矩阵即可

        因为题目中的地图是一个规则的二维网格(岛屿问题、迷宫问题),每个格子与上下左右四个相邻格子直接相连,这种结构天然适合用二维数组表示。并且通过二维数组可以直接访问任意位置的格子状态,就无需额外的逻辑转换

        为什么不适用邻接矩阵或者邻接表?

        使用邻接矩阵需要存储所有顶点的连接关系 而网格中 每个格子仅与固定方向的邻居相连,会导致大量空间浪费

        使用邻接表需为每个顶点维护链表,但网格中顶点的邻居位置可通过坐标计算直接获取(如(x±1, y±1)),无需额外存储边信息

2.每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

   所以斜着角度连接的就不能算了 可以只考虑上下左右的格子

3.写DFS时候想着深搜三部曲:

确认递归函数和参数,确认终止条件,处理目前搜索节点出发的路径

本题思路:遇见一个没有遍历过的节点陆地 result计数器++ 然后把和该陆地相连的所有陆地都标记上(已访问) 在遇见标记过的陆地节点和海洋节点时候就跳过

        

代码实现

我的代码实现:

#include
using namespace std;
int n, m;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
void dfs(vector> &grid, vector> &visited,int x,int y){
    if(grid[x][y] == 0 || visited[x][y] == true)return ;
    visited[x][y] = true;
    for(int i = 0; i < 4;i++){
       int nextx = x + dir[i][0];
       int nexty = y + dir[i][1];
        if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;
        
        dfs(grid,visited,nextx,nexty);
    }

}
int main(){
    cin >> n >> m;
    vector> grid(n,vector(m,0));
    vector> visited(n,vector(m,false));
    // 用朴素方式存储表格
    for(int i = 0 ;i < n;i++){  
        for(int j = 0; j < m; j++){
            cin >> grid[i][j];
        }
    }
    int res = 0;
    // 遍历每个格子
    for(int i = 0;i < n;i++){  
        for(int j = 0; j < m; j++){
            if(visited[i][j] == false && grid[i][j] == 1){
                res ++;
                dfs(grid,visited,i,j);
            }
        }
    }
    cout << res;

    return 0;
}

卡哥给出的代码实现:

#include 
#include 
using namespace std;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void dfs(const vector>& grid, vector>& visited, int x, int y) {
    if (visited[x][y] || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水
    visited[x][y] = true; // 标记访问过
    for (int i = 0; i < 4; i++) {
        int nextx = x + dir[i][0];
        int nexty = y + dir[i][1];
        if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;  // 越界了,直接跳过
        dfs(grid, visited, nextx, nexty);
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    vector> grid(n, vector(m, 0));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> grid[i][j];
        }
    }

    vector> visited(n, vector(m, false));

    int result = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (!visited[i][j] && grid[i][j] == 1) {
                result++; // 遇到没访问过的陆地,+1
                dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
            }
        }
    }
    cout << result << endl;
}

BFS方法:

void bfs(vector> &grid, vector> &visited,int x,int y){
    queue> que;
    que.push({x,y});
    visited[x][y] = true;
    while(!que.empty()){
        pair cur = que.front(); que.pop();
    	for(int i = 0; i<4;i++){
            int nx = cur.first + dir[i][0];
             int ny = cur.second + dir[i][1];
            if(nx < 0 || ny <0 || nx >= grid.size() || ny>= grid[0].size())continue;
            if(grid[nx][ny] == 1 && visited[nx][ny] == false){
              que.push({nx,ny});
                 visited[nx][ny] = true;
        	}	
		}
    }
}

卡哥BFS方法

int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void bfs(vector>& grid, vector>& visited, int x, int y) {
    queue> que;
    que.push({x, y});
    visited[x][y] = true; // 只要加入队列,立刻标记
    while(!que.empty()) {
        pair cur = que.front(); que.pop();
        int curx = cur.first;
        int cury = cur.second;
        for (int i = 0; i < 4; i++) {
            int nextx = curx + dir[i][0];
            int nexty = cury + dir[i][1];
            if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;  // 越界了,直接跳过
            if (!visited[nextx][nexty] && grid[nextx][nexty] == '1') {
                que.push({nextx, nexty});
                visited[nextx][nexty] = true; // 只要加入队列立刻标记
            }
        }
    }

}

你可能感兴趣的:(深度优先,算法,岛屿数量,图论)