(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 ...
随机推荐
- idea中Lombok的使用
使用了lombok的注解(@Setter,@Getter,@ToString,@@RequiredArgsConstructor,@EqualsAndHashCode或@Data)之后,就不需要编写或 ...
- Cherry.chen window.clipboardData实现剪切板操作总结 (好像只有ie好用)
window.clipboardData的作用是在页面上将需要的东西复制到剪贴板上,提供了对于预定义的剪贴板格式的访问,以便在编辑操作中使用. 三个方法 (1)clearData(sDataForma ...
- How to vi
h:left,j:down,k:up,l:right.wq #write and quitx #cut one letterdd#cut one line/ #searchs/a/b/ #replac ...
- Redux学习(2) ----- 异步和中间件
Redux中间件,其实就是一个函数, 当我们发送一个action的时候,先经过它,我们就可以对action进行处理,然后再发送action到达reducer, 改变状态,这时我们就可以在中间件中,对a ...
- Root test & Ratio test
几何级数(Geometric Series/Geometric Progression) Root test与Ratio test都依赖于几何级数求和理论,因此这里先讨论该理论. 在数学上,几何级数, ...
- net core 2.0 + Autofac的坑
控制器不能从容器中解析出来; 只是控制器构造函数参数.这意味着控制器生命周期,属性注入和其他事情不由Autofac管理 - 它们由ASP.NET Core管理.可以通过指定AddControllers ...
- P1055 书号
P1055 题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括99位数字.11位识别码和33位分隔符,其规定格式如x-xxx-xxxxx-x,其中符号-就是分隔符(键盘上的减号 ...
- BZOJ 1912 巡逻(算竞进阶习题)
树的直径 这题如果k=1很简单,就是在树的最长链上加个环,这样就最大化的减少重复的路程 但是k=2的时候需要考虑两个环的重叠部分,如果没有重叠部分,则和k=1的情况是一样的,但是假如有重叠部分,我们可 ...
- Gogs 部署安装(windows)
Gogs简介 Gogs 是一款类似GitHub的开源文件/代码管理系统(基于Git),Gogs 的目标是打造一个最简单.最快速和最轻松的方式搭建自助 Git 服务.使用 Go 语言开发使得 Gogs ...
- HDU6341 Let Sudoku Rotate (杭电多校4J)
给一个由4*4个4*4的小格组成数独,这些数独是由一个块逆时针旋转得来的,所以要还原的话就模拟出顺时针的过程,先把里面的字母转化成数字,然后从第一个块开始枚举,每个dfs和之前枚举的已经满足条件的块, ...