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. LuoguP7478 【A】StickSuger 题解

    Content 给定一个长度为 \(n\) 的仅包含小写字母的字符串 \(s\),请找到一个二元组 \((i,j)\)(\(i<j\))使得在交换字符串 \(s\) 的第 \(i\) 个和第 \ ...

  2. Python3 shevel模块,更高级的json序列化数据类型模块(比pickle更高级)

    直接将数据类型以字典的格式 存到文件中去. 直接.get读取出来,

  3. 使用docker自定义oraclejdk启动jar包

    Dockerfile文件 FROM centos:7 #把java与tomcat添加到容器中 ADD jdk-8u161-linux-x64.tar.gz /usr/local/ #安装 vim编辑器 ...

  4. 【LeetCode】997. Find the Town Judge 解题报告(C++)

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

  5. 【剑指Offer】不用加减乘除做加法 解题报告(Java)

    [剑指Offer]不用加减乘除做加法 解题报告(Java) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题 ...

  6. Codeforces 876B:Divisiblity of Differences(数学)

    B. Divisiblity of Differences You are given a multiset of n integers. You should select exactly k of ...

  7. Codeforces Gym-100985C: MaratonIME plays Nim(交互题&博弈)

    C. MaratonIME plays Nim time limit per test : 2.0 smemory limit per test : 64 MBinputstandard inputo ...

  8. FP增长算法

    Apriori原理:如果某个项集是频繁的,那么它的所有子集都是频繁的. Apriori算法: 1 输入支持度阈值t和数据集 2 生成含有K个元素的项集的候选集(K初始为1) 3 对候选集每个项集,判断 ...

  9. 使用 Android Studio 开发工具创建一个 Android 应用程序,并在 Genymotion 模拟器上运行

    需求说明: 使用 Android Studio 开发工具创建一个 Android 应用程序,并在 Genymotion 模拟器上运行 实现步骤: 打开 Android Studio,创建一个 Andr ...

  10. 使用 jQuery 实现页面背景色的更换,通过下拉框选择对应的颜色,页面背景会随着选中的颜色进行更换

    查看本章节 查看作业目录 需求说明: 使用 jQuery 实现页面背景色的更换,通过下拉框选择对应的颜色,页面背景会随着选中的颜色进行更换 实现思路: 在页面中添加 <select> 标签 ...