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(意思是,如果仅仅是斜角都为1,不是ajacent land). You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
11010
11000
00000

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

思路:找到没有标志的1,表示找到了一个island,然后找它的连通图,把这个连通图里的1都设置标志。

class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int hight = grid.size();
if(hight == ) return ;
int width = grid[].size();
int ret = ; for(int i = ; i < hight; i++){
for(int j = ; j < width; j++){
if(grid[i][j]=='') continue; //find a land, continue to find land at its side
ret++;
grid[i][j] = ''; //set flag
dfs(grid, i+, j);
dfs(grid, i, j+);
dfs(grid, i-, j);
dfs(grid, i, j-);
}
}
return ret;
} void dfs(vector<vector<char>>& grid, int i, int j){
int hight = grid.size();
int width = grid[].size(); if(i>=hight || j>= width || i< || j<) return;
if(grid[i][j]=='') return; //find a land, continue to find land at its side
grid[i][j] = ''; //set flag
dfs(grid, i+, j);
dfs(grid, i, j+);
dfs(grid, i-, j);
dfs(grid, i, j-);
}
};

200. Number of Islands (Graph)的更多相关文章

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

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

  7. 200. Number of Islands(DFS)

    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(其实就是一个深搜)

    题目: 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岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. kubernetes国内镜像拉取

    因国内访问不到goole服务器,只能拉取国内的镜像,这里以阿里云为例. 安装minikube时报failed to pull image "k8s.gcr.io/kube-apiserver ...

  2. Django 之多表查询 与多表的使用

    1.django的多表查询 主要区分为: 正向查询    逆向查询 1. 多表查询: 是一个复杂的查询,他分为对象查询和__模糊查询两种方式 2. 多表查询: 又分为 一对一查询, 一对多查询, 多对 ...

  3. visio交叉线不凸起

    使用visio作图时,经常会遇到交叉线在相交时会形成一个弯曲弓形,这有时十分影响视图效果.可以采用下面的方法消除弓形. 1.visio2003:只需要选中该交叉线,选择“格式”->“行为”,在打 ...

  4. mysql 索引优化知识整理笔记

    http://blog.csdn.net/zhxp_870516/article/details/8434539 http://www.jb51.net/article/49346.htm https ...

  5. echarts-颜色渐变

    图形的颜色. 默认从全局调色盘 option.color 获取颜色 颜色可以使用 RGB 表示,比如 'rgb(128, 128, 128)',如果想要加上 alpha 通道表示不透明度,可以使用 R ...

  6. python 可迭代对象与迭代器

    生成器函数的工作原理只要 Python 函数的定义体中有 yield 关键字, 该函数就是生成器函数. 调用生成器函数时, 会返回一个生成器对象. 也就是说, 生成器函数是生成器工厂. 调用生成器函数 ...

  7. ACM__搜素之BFS与DFS

    BFS(Breadth_First_Search) DFS(Depth_First_Search) 拿图来说 BFS过程,以1为根节点,1与2,3相连,找到了2,3,继续搜2,2与4,相连,找到了4, ...

  8. CocosCreator 自定义TypeScript在VsCode的提示数据

    在assets文件夹外新建xx.d.ts文件如:global.d.ts global.d.ts declare class UserData{ node:cc.Node; name:string; } ...

  9. Stephen Hawking Taught Us a Lot About How to Live

    勇气.好奇心.幽默感,那些霍金教给我们的事Stephen Hawking Taught Us a Lot About How to LiveStephen Hawking, the English c ...

  10. Pronunciation Guide for 25 Common Fruits

    Pronunciation Guide for 25 Common Fruits Share Tweet Share Tagged With: Vocabulary Words Know how to ...