赞
踩
给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
示例 1: 输入:grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] 输出:1 示例 2: 输入:grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] 输出:3 提示: m == grid.length n == grid[i].length 1 <= m, n <= 300 grid[i][j] 的值为 '0' 或 '1'
题目是找到矩阵中 “岛屿的数量” ,因此上下左右相连的’1’都被认为是同一个连续的岛屿。
1.主循环:遍历整个矩阵,当遇到 grid[i][j] == ‘1’ 时,从此点开始做深度优先搜索 dfs,岛屿数 count + 1 且在深度优先搜索中删除此岛屿。
2.bfs方法:借用一个队列queue,将未越界且为字符’1’的节点 (i, j)加入队列:
具体实现代码如下:
class Solution { public: void bfs(vector<vector<char>>& grid,int i,int j,int cow,int col) { queue<vector<int>> curQue; curQue.emplace(vector<int>{i,j}); while(!curQue.empty()) { auto curNode = curQue.front(); curQue.pop(); int m=curNode[0],n=curNode[1]; if(m-1>=0 && grid[m-1][n] == '1') { grid[m-1][n] = '0'; curQue.emplace(vector<int>{m-1,n}); } if(m+1<cow && grid[m+1][n] == '1') { grid[m+1][n] = '0'; curQue.emplace(vector<int>{m+1,n}); } if(n-1>=0 && grid[m][n-1] == '1') { grid[m][n-1] = '0'; curQue.emplace(vector<int>{m,n-1}); } if(n+1<col && grid[m][n+1] == '1') { grid[m][n+1] = '0'; curQue.emplace(vector<int>{m,n+1}); } } } int numIslands(vector<vector<char>>& grid) { int count=0,cow=grid.size(),col=grid[0].size(); for(int i=0;i<cow;++i) { for(int j=0;j<col;++j) { if('1' == grid[i][j]) { grid[i][j] = '0'; bfs(grid,i,j,cow,col); ++count; } } } return count; } };
AC结果:
1.主循环:遍历整个矩阵,当遇到 grid[i][j] == ‘1’ 时,从此点开始做深度优先搜索 dfs,岛屿数 count + 1 且在深度优先搜索中删除此岛屿。
2.dfs方法: 设目前指针指向一个岛屿中的某一点 (i, j),寻找包括此点的岛屿边界。
具体实现代码如下:
class Solution { public: void dfs(vector<vector<char>>& grid,int i,int j) { if(i<0 || i>=grid.size() || j<0 || j>=grid[i].size() || grid[i][j]=='0' ) return; grid[i][j]='0'; dfs(grid,i,j-1); dfs(grid,i,j+1); dfs(grid,i-1,j); dfs(grid,i+1,j); } int numIslands(vector<vector<char>>& grid) { int nr = grid.size(); if (nr == 0) return 0; int nc = grid[0].size(); int count=0; for(int i=0;i<nr;++i) { for(int j=0;j<nc;++j) { if('1' == grid[i][j]) { dfs(grid,i,j); ++count; } } } return count; } };
AC结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。