题意: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. SpringJUnit4测试--测试无反应/控制台报空指针的解决---junit的jar冲突!

    前言: 前些日子碰到一个诡异的问题--用springJUnit进行测试,运行方法什么反应也没有,控制台 也没有输出,百度也没有答案--只好暂时作罢.今天我只好用上了排除法,建个测试小项目,将只要能测试 ...

  2. 【贪心】Bzoj 2457:[BeiJing2011]双端队列

    2457: [BeiJing2011]双端队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 209  Solved: 95[Submit][Stat ...

  3. Matlab中find函数的使用

    一.问题来源 看到了 min_score_pos = find(A0_scores==min(A0_scores), 1); [r,c] = find(X,k),返回X中第k个非零元素的行列位置. 二 ...

  4. java内存分配详细论

    P.S. 想写这篇总结酝酿了有个来月了,却始终感觉还差点什么东西,一直未敢动笔. 最近两天连夜奋战,重新整理下前面查阅的资料.笔记,还是决定将它写出来. 现在提出几个问题,如果都能熟练回答的大虾,请您 ...

  5. Java 理论和实践: 了解泛型

    转载自 : http://www.ibm.com/developerworks/cn/java/j-jtp01255.html 表面上看起来,无论语法还是应用的环境(比如容器类),泛型类型(或者泛型) ...

  6. java基础知识回顾之javaIO类--java序列化和反序列化

    /** *  * 一:理解序列化反序列化及其应用 * 序列化:把堆内存的对象转化成字节流的过程. * 反序列化:把字节流序列恢复重构成对象的过程. * 对象的序列化的用途:1.把对象的字节序列持久化, ...

  7. the-type-java-lang-charsequence-cannot-be-resolved-in-package-declaration

    http://stackoverflow.com/questions/24301986/the-type-java-lang-charsequence-cannot-be-resolved-in-pa ...

  8. struts2学习笔记(4)——数据类型转换

    回过头来看昨天的那个例子. 在昨天的例子中,只转换了一个Point类,如果想转换多个Point类怎么办呢?在昨天的例子上面做一个小的修改. 首先在input.jsp页面中修改几个输入框. <s: ...

  9. VNC Server 配置

    1. 检查vnc客户端和服务器是否已经安装: [gavin@centos ~]$ rpm -q vnc vnc-server package vnc is not installed vnc-serv ...

  10. spring 与 CXF 整合 webservice 出现error “Unable to locate Spring NamespaceHandler for XML schema namespace” 总结

    我试了多个版本的spring 发现 出现error : Unable to locate Spring NamespaceHandler for XML schema namespace 并非都是sp ...