题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛?

思路:DFS还是比较短,实现了一下。如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的。

 class Solution {
public:
bool isok(vector<vector<char> >& grid,int x,int y)
{
return x>= && y>= && x<grid.size() && y<grid[].size();
} void DFS( vector<vector<char> >& grid,int x,int y )
{
if( !isok(grid,x,y) || grid[x][y]=='' ) return ; grid[x][y]='';
DFS( grid, x, y+ );
DFS( grid, x, y- );
DFS( grid, x+, y );
DFS( grid, x-, y );
} int numIslands(vector<vector<char> >& grid) {
int ans=;
for(int i=; i<grid.size(); i++)
{
for(int j=; j<grid[].size(); j++)
{
if(grid[i][j]=='')
{
DFS(grid, i, j);
ans++;
}
}
}
return ans;
}
};

AC代码

LeetCode Number of Islands 岛的数量(DFS,BFS)的更多相关文章

  1. [LeetCode] 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 ...

  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] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  4. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  5. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  6. [leetcode] Number of Islands

    Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

  7. Leetcode: Number of Islands II && Summary of Union Find

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  8. LeetCode – Number of Islands II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  9. LeetCode – Number of Islands

    Given a -d grid map of 's (water), count the number of islands. An island is surrounded by water and ...

随机推荐

  1. execvp使用实例

    问题描述:           本程序实现模拟shell功能,用户输入命令,返回相应的结果 问题解决: 注:        以上指出了execvp函数的使用,使用时第一个参数是文件名,第二个参数是一个 ...

  2. iOS开发网络编程之断点续传-NSURLConnection

    最近在做一个小项目的时候,发现使用NSURLSession或者AFNNetworking进行断点续传时诸多的不便,于是自己封装了一个类来实现断点续传,在程序重新启动时仍然可以继续下载(需自己调用方法) ...

  3. matlab中读取txt数据文件(txt文本文档)

    matlab中读取txt数据文件(txt文本文档) 根据txt文档不同种类介绍不同的读取数据方法 一.纯数据文件(没有字母和中文,纯数字) 对于这种txt文档,从matalb中读取就简单多了 例如te ...

  4. Ignore files which are already versioned

    If you accidentally added some files which should have been ignored, how do you get them out of vers ...

  5. C# DataTable转换成DataRow

    linq中的cast<T>()及OfType<T>() DataTable dt=...........//获取从数据库中取出的数据(假设只有一条记录) //Cast<T ...

  6. sun.misc.unsafe类的使用

    http://blog.csdn.net/fenglibing/article/details/17138079

  7. POJ2418Hardwood Species

    题意 : 输入若干个树木品种,里边有的是重复的,让你统计每个品种占输入的总数量的百分比,最后按字典序输出 思路 : 本题数量很大,所以采用直接统计的方式会超时,而采用的方法可以用二叉搜索树,或者用ma ...

  8. POJ1004Financial Management

    这个题犯了一个小小的错误,double输出的时候用f才对,输入用lf即可.... http://poj.org/problem?id=1004 #include<stdio.h> int ...

  9. 为什么Android 3.0如此罕见?

    3.0(2011年2月)代号蜂巢,专用于android系统的平板电脑,不用于手机.4.0(2011年5月公布)的开发就是让平板电脑和手机能够共用一个版本的系统.4.0通用于平板电脑和手机.

  10. hdu1017

    http://acm.hdu.edu.cn/showproblem.php?pid=1017 #include<iostream> #include<stdio.h> #inc ...