LeetCode Number of Islands 岛的数量(DFS,BFS)
题意: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)的更多相关文章
- [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 ...
 - [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 ...
 - [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 ...
 - [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)
		
695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...
 - [LintCode] Number of Islands 岛屿的数量
		
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
 - [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. ...
 - 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 ...
 - 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 ...
 - LeetCode – Number of Islands
		
Given a -d grid map of 's (water), count the number of islands. An island is surrounded by water and ...
 
随机推荐
- 我的第一个python爬虫程序
			
程序用来爬取糗事百科上的图片的,程序设有超时功能,具有异常处理能力 下面直接上源码: #-*-coding:utf-8-*- ''' Created on 2016年10月20日 @author: a ...
 - java和javascript真的有关系=。=
			
相同点:1. 内存管理,两者都采用GC来对内存进行回收.因此Java与javascript的内存泄露情况十分相似. 2. 代码编译为机器码后由中间件执行:Java使用前会编译为字节码后由JVM执行,V ...
 - hibernate Session
			
转: http://kayo.iteye.com/blog/204143 Session 接口 Session 接口对于Hibernate 开发人员来说是一个最重要的接口.然而在Hibernate 中 ...
 - LA 4329
			
第一次敲树状数组 因为一个小错误 wa了 n 多遍 终于ac 太不容易了 /*********************************************************** ...
 - POJ 1417 True Liars(种类并查集+dp背包问题)
			
题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...
 - GitHub 开源工具整理
			
技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddit:同上 MSDN:微软相关的官方技术集中地,主要是文档类 infoq:企业级应用,关注软件开发领域 ...
 - mysql 多表 update sql语句总结
			
mysql 多表 update 有几种不同的写法. 假定我们有两张表,一张表为Product表存放产品信息,其中有产品价格列Price:另外一张表是ProductPrice表,我们要将ProductP ...
 - 网络上下载的Ghost系统含威胁
 - log4j的基本配置参数
			
转载:http://blog.csdn.net/fengyifei11228/article/details/6070006 log4j配置文件有三个主要的组件:Logger,Appender和Lay ...
 - 一个简单的将GUI程序的log信息输出到关联的Console窗口中(AllocConsole SetConsoleTitle WriteConsole 最后用ShowWindow(GetConsoleWindow)进行显示)
			
// .h 文件 #pragma once class CConsoleDump { public: explicit CConsoleDump(LPCTSTR lpszWindowTitle = N ...