给你一个由'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
-
grid[i][j]
的值为'0'
或'1'
思路:采用宽度优先算法,大体思路套两层循环,并用一个bool型数组记录每个位置是否服务器托管网被遍历过,对没遍历过的位置采用bfs。
代码:
class Solution {
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool arr[301][301]={0};
public:
int numIslands(vector>& grid) {
int m=grid.size(),n=grid[0].size();
int count=0;
for(int i=0;i>& v,int i,int j)
{
queue> q;
q.push({i,j});
while(q.size())
{
auto [a,b]=q.front();
q.pop();
arr[a][b]=true;
for(int k=0;k=0&&x=0&&y
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: OpenSergo & Dubbo 微服务治理最佳实践
12.23 源创会 上海站,聊聊 LLM 基础设施 *作者:何家欢,阿里云 MSE 研发工程师 Why 微服务治理? 现代的微服务架构里,我们通过将系统分解成一系列的服务并通过远程过程调用联接在一起,在带来一些优势的同时也为我们带来了一些挑战。 如上图所示,可…