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

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

  1. class Solution {
  2. public:
  3. bool isok(vector<vector<char> >& grid,int x,int y)
  4. {
  5. return x>= && y>= && x<grid.size() && y<grid[].size();
  6. }
  7.  
  8. void DFS( vector<vector<char> >& grid,int x,int y )
  9. {
  10. if( !isok(grid,x,y) || grid[x][y]=='' ) return ;
  11.  
  12. grid[x][y]='';
  13. DFS( grid, x, y+ );
  14. DFS( grid, x, y- );
  15. DFS( grid, x+, y );
  16. DFS( grid, x-, y );
  17. }
  18.  
  19. int numIslands(vector<vector<char> >& grid) {
  20. int ans=;
  21. for(int i=; i<grid.size(); i++)
  22. {
  23. for(int j=; j<grid[].size(); j++)
  24. {
  25. if(grid[i][j]=='')
  26. {
  27. DFS(grid, i, j);
  28. ans++;
  29. }
  30. }
  31. }
  32. return ans;
  33. }
  34. };

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. Problem 1016 咒文卷轴 优先队列+前缀和+rmq

    题目链接: 题目 Problem 1016 咒文卷轴 Time Limit: 3000 mSec Memory Limit : 131072 KB 问题描述 小Y 是一个魔法师,有一天他获得了一卷神秘 ...

  2. JS实现Web网页打印功能(IE)

    问题描述:     JS实现Web网页打印功能 问题解决:     这里主要使用WebBrowser控件的ExeWB在IE中打印功能的实现 WebBrowser介绍:         WebBrows ...

  3. log4j安装与简介

    问题描述:     在应用程序中添加日志记录总的来说基于三个目的:    (1) 监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作:     (2) 跟踪代码运行时轨迹,作为日 ...

  4. 暑假学习日记:Splay树

    从昨天开始我就想学这个伸展树了,今天花了一个上午2个多小时加下午2个多小时,学习了一下伸展树(Splay树),学习的时候主要是看别人博客啦~发现下面这个博客挺不错的http://zakir.is-pr ...

  5. POJ1004Financial Management

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

  6. dynamic介绍

    Visual C# 2010 引入了一个新类型 dynamic. 该类型是一种静态类型,但类型为 dynamic 的对象会跳过静态类型检查. 大多数情况下,该对象就像具有类型 object 一样. 在 ...

  7. 【POJ3358】

    题目描述: 题意: 就是给定一个a/b,求a/b的结果变成二进制之后的小数.这个小数后面会有一段循环节,只要求输出循环节开始循环的位置和循环长度. 分析: 这题我是这么想的,比如说样例中的1/5,我们 ...

  8. lintcode:哈希函数

    题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...

  9. HTML5入门九---Canvas画布

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. Hibernate逍遥游记-第15章处理并发问题-002悲观锁

    1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...