1. 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: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output: 1

Example 2:

Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3

解1 bfs

class Solution {
public:
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool valid(int x, int y, int m, int n){
if(x < 0 || x >= m || y < 0 || y >= n)return false;
return true;
}
int numIslands(vector<vector<char>>& grid) {
if(grid.size() == 0)return 0;
vector<vector<bool>> vis(grid.size(), vector<bool>(grid[0].size(), false));
int ans = 0;
for(int i = 0; i < grid.size(); ++i){
for(int j = 0; j < grid[0].size(); ++j){
if(vis[i][j] == false && grid[i][j] == '1'){
ans++;
bfs(grid, vis, i, j);
//dfs(grid, vis, i, j);
}
}
}
return ans;
}
void bfs(vector<vector<char>>& grid, vector<vector<bool>>& vis,
int x, int y){
queue<pair<int, int>>q;
q.push(make_pair(x,y));
vis[x][y] = true;
while(!q.empty()){
int tmpx = q.front().first, tmpy = q.front().second;
q.pop();
for(int i = 0; i < 4; ++i){
int tmpxx = tmpx + dx[i], tmpyy = tmpy + dy[i];
if(valid(tmpxx, tmpyy, grid.size(), grid[0].size())
&& !vis[tmpxx][tmpyy] && grid[tmpxx][tmpyy]=='1'){
q.push(make_pair(tmpxx, tmpyy));
vis[tmpxx][tmpyy] = true;
}
}
}
}
};

解2 dfs

	void dfs(vector<vector<char>>& grid, vector<vector<bool>>& vis,
int x, int y){
vis[x][y] = true;
for(int i = 0; i < 4; ++i){
int tmpx = x + dx[i], tmpy = y + dy[i];
if(valid(tmpx, tmpy, grid.size(), grid[0].size())
&& !vis[tmpx][tmpy] && grid[tmpx][tmpy] == '1'){
dfs(grid, vis, tmpx, tmpy);
}
}
}

【刷题-LeetCode】200 Number of Islands的更多相关文章

  1. 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 用了第一种方式, ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. (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 surro ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. Leetcode 200 Number of Islands DFS

    统计联通区域块的个数,简单dfs,请可以参考DFS框架:Leetcode 130 Surrounded Regions DFS class Solution { public: int m, n; b ...

  10. [leetcode]200. Number of Islands岛屿数量

    dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...

随机推荐

  1. 以太网/ IPV4/IPV6包头,TCP包头格式回顾

    问题:以太网数据包,承载的数据内容大小46~1500字节,是如何来的? 以太网数据包结构  以太网协议规定最小链路层数据包(帧)为64字节,其中以太网首部+尾部共计18字节(源/目的MAC12字节:上 ...

  2. curl -w参数简析

    curl的-w参数用于在一次完整且成功的操作后输出指定格式的内容到标准输出. 输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{variable_name}的格式,如果需要输出%,doub ...

  3. mysql联合查询更新数据库例子

    mysql联合查询更新数据库例子,用户表,部门表,把用户表中的部门属性更新为部门表的主键UPDATE user_table AS utINNER JOIN belongdept AS bd ON bd ...

  4. Springboot在工具类(Util)中使用@Autowired注入Service

    1. 使用@Component注解标记工具类MailUtil: 2. 使用@Autowired注入我们需要的bean: 3. 在工具类中编写init()函数,并使用@PostConstruct注解标记 ...

  5. Linux(centos)使用docker安装pdf2htmlEX

    pdf2htmlEX是一款可以将pdf文档转换成html文件的插件,但是Linux系统安装起来很麻烦,所以我们使用docker进行安装 首先要安装docker 因为国外镜像很慢,所以我们这边修改使用国 ...

  6. JAVA通过经纬度获取两点之间的距离

    private static double EARTH_RADIUS = 6378.137; private static double rad(double d) { return d * Math ...

  7. Mac下好用的“visio”之 OmniGraffle Pro

    !!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist 1.官方网站:https://www.omnigroup.com/omnigraffle/ ...

  8. C语言获取文件大小(字节)

    代码 核心代码 FILE *pfile = nullptr; int ret = fopen_s(&pfile, str.c_str(), "rb"); /// 0 = 打 ...

  9. 【LeetCode】981. Time Based Key-Value Store 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  10. Saving Beans(hud3037)

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...