(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 ...
随机推荐
- jqGrid选中行、格式化、自定义按钮、隐藏
获取选择一行的id: var id=$('#jqGrid').jqGrid('getGridParam','selrow'); 获取选择多行的id: var ids=$('#jqGrid').jqGr ...
- APP需求调研、对比
二.人脸验证 1.芝麻认证 : 0.4元/次,需要企业企业认证.不能有与芝麻信用类似的业务,如:保险... 2.旷视 : 0.5/次.企业认证.业务限制 3. 百度人脸识别 : 企业认证. 4.科大 ...
- sws_getContext函数参数介绍
原型: SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, ...
- vs + babelua + cocos2d-x
https://blog.csdn.net/dugaoda/article/details/60467037 https://blog.csdn.net/taotanty/article/detail ...
- PHP——emjoin表情存入数据库
前言 还有一种解决的方法是更改数据库,这里就不写了,这里直接对emoji进行转码 代码 mb_strlen() | strlen() | rawurlencode() | rawurldecode() ...
- POJ3261-Milk Patterns-后缀数组
可重叠重复k次的最长子串长度. 还是使用二分答案对heigh数组分组的做法. #include <cstdio> #include <algorithm> #include & ...
- Python中的urllib2模块解析
Name urllib2 - An extensible library for opening URLs using a variety of protocols 1. Description Th ...
- Linux CAT与ECHO命令详解
Linux CAT与ECHO命令详解 cat命令是Linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: 1.一次显示整个文件. $ cat filename 2.从 ...
- Codeforces518 D. Ilya and Escalator
传送门:>Here< 题意:有n个人排队做电梯,每个人必须等前面的人全部上了以后才能上.对于每秒钟,有p的概率选择上电梯,(1-p)的概率选择不上电梯.现在问t秒期望多少人上电梯 解题思路 ...
- 【BZOJ3816】【清华集训2014】矩阵变换 稳定婚姻问题
题目描述 给出一个\(n\)行\(m\)列的矩阵\(A\), 保证满足以下性质: 1.\(m>n\). 2.矩阵中每个数都是\([0,n]\)中的自然数. 3.每行中,\([1,n]\)中每个自 ...