(BFS/DFS) leetcode 200. Number of Islands
Given a 2d grid map of '1'
s (land) and '0'
s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input:
11110
11010
11000
00000 Output: 1
Example 2:
Input:
11000
11000
00100
00011 Output: 3
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
typedef pair<int,int> pii;
queue<pii> q;
int x = ,y = ,xx = ,yy = ;
int dx[] = {,-,,};
int dy[] = {,,,-};
int row = grid.size();
int col;
int sum = ;
if(row > ){
col = grid[].size();
}
else{
col = ;
}
if(row == || col == ){
return ;
}
for(int i = ; i < row; i++){
for(int j = ; j < col; j++){
if(grid[i][j] == ''){
grid[i][j] == '';
q.push(pii(i,j));
while(!q.empty()){
x = q.front().first;
y = q.front().second;
q.pop();
for(int i = ; i < ; i++){
xx = x + dx[i];
yy = y + dy[i];
if(xx >= && xx < row && yy >= && yy < col && grid[xx][yy] == ''){
grid[xx][yy] = '';
q.push(pii(xx,yy));
}
}
}
sum++;
}
}
}
return sum;
}
};
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
if(grid.size() == || grid[].size() == ) return ;
int m = grid.size();
int n = grid[].size();
int res = ;
vector<vector<bool> > vis(m,vector<bool>(n,false));
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
if(!vis[i][j] && grid[i][j] == ''){
DFS(grid,vis,i,j);
res++;
}
}
}
return res;
}
void DFS(vector<vector<char> >& grid,vector<vector<bool> >& vis, int x, int y){
if(x < || x >= grid.size()) return;
if(y < || y >= grid[].size()) return;
if(grid[x][y] != '' || vis[x][y]) return;
vis[x][y] = true;
DFS(grid,vis,x+,y);
DFS(grid,vis,x-,y);
DFS(grid,vis,x,y+);
DFS(grid,vis,x,y-);
}
};
(BFS/DFS) leetcode 200. Number of Islands的更多相关文章
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- [LeetCode] 200. Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Leetcode 200. number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [leetcode]200. Number of Islands岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LeetCode] 200. Number of Islands 解题思路
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- LeetCode 200. Number of Islands 岛屿数量(C++/Java)
题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...
- Leetcode 200 Number of Islands DFS
统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...
- [leetcode]200. Number of Islands岛屿数量
dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...
随机推荐
- 14.statefulset服务
有状态的控制器有以下几个特点 稳定,独特的网络标识符. 稳定,持久的存储. 有序,优雅的部署和扩展. 有序的自动滚动更新. 使用限制 StatefulSet是1.9之前的beta资源,在1.5之前的任 ...
- DatasourceUtils类:获取连接池和数据库连接
本工具类用于获取连接池和数据库连接 package com.itheima.utils; import java.sql.Connection; import java.sql.ResultSet; ...
- kubernetes 创建系统用户来支持访问 dashboard
Dashboard: 1.部署: 下载yaml文件 可以直接运行也可以下载下来kubectl apply -f https://raw.githubusercontent.com/kubernete ...
- Codeforces Round #423 Div. 1
A:暴力赋值即可,并查集维护下一个未被赋值的位置. #include<iostream> #include<cstdio> #include<cmath> #inc ...
- Lambda 动态表达式(排序)
网上看到的: class Program { static List<User> list = new List<User>() { new User(){ID=1,Name= ...
- ContOS安装配置MySQL,redis
MySQL(MariaDB) 一,说明 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MyS ...
- Python中xlwt解析
1.导入模块 import xlwt 2.构造excel表 workbook = xlwt.Workbook() #返回一个工作簿对象 3.构造sheet w ...
- BZOJ3277 串 【后缀数组】【二分答案】【主席树】
题目分析: 用"$"连接后缀数组,然后做一个主席树求区间内不同的数的个数.二分一个前缀长度再在主席树上求不同的数的个数. 代码: #include<bits/stdc++.h ...
- 洛谷P1622释放囚犯
题目: 这个题很明显是一个区间DP,但是比较不同的是,这个题它很像区间DP的经典题——石子合并. 然后我傻傻的搞了这个题搞了一下午,然后几乎看遍了全网的题解,就只看懂了这个方法,可能是我太菜了吧,但是 ...
- 【XSY2729】欧拉子图 无向图连通性 数学
题目大意 给你一个\(n\)个点\(m\)条边的无向图(可能有重边),对于这个图的边集的子集(一共有\(2^m\)个),如果其导出的子图的每个联通块内都存在欧拉回路,我们就把答案加上这个子图的边数的平 ...