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 ...
随机推荐
- Problem 1016 咒文卷轴 优先队列+前缀和+rmq
题目链接: 题目 Problem 1016 咒文卷轴 Time Limit: 3000 mSec Memory Limit : 131072 KB 问题描述 小Y 是一个魔法师,有一天他获得了一卷神秘 ...
- JS实现Web网页打印功能(IE)
问题描述: JS实现Web网页打印功能 问题解决: 这里主要使用WebBrowser控件的ExeWB在IE中打印功能的实现 WebBrowser介绍: WebBrows ...
- log4j安装与简介
问题描述: 在应用程序中添加日志记录总的来说基于三个目的: (1) 监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作: (2) 跟踪代码运行时轨迹,作为日 ...
- 暑假学习日记:Splay树
从昨天开始我就想学这个伸展树了,今天花了一个上午2个多小时加下午2个多小时,学习了一下伸展树(Splay树),学习的时候主要是看别人博客啦~发现下面这个博客挺不错的http://zakir.is-pr ...
- POJ1004Financial Management
这个题犯了一个小小的错误,double输出的时候用f才对,输入用lf即可.... http://poj.org/problem?id=1004 #include<stdio.h> int ...
- dynamic介绍
Visual C# 2010 引入了一个新类型 dynamic. 该类型是一种静态类型,但类型为 dynamic 的对象会跳过静态类型检查. 大多数情况下,该对象就像具有类型 object 一样. 在 ...
- 【POJ3358】
题目描述: 题意: 就是给定一个a/b,求a/b的结果变成二进制之后的小数.这个小数后面会有一段循环节,只要求输出循环节开始循环的位置和循环长度. 分析: 这题我是这么想的,比如说样例中的1/5,我们 ...
- lintcode:哈希函数
题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...
- HTML5入门九---Canvas画布
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Hibernate逍遥游记-第15章处理并发问题-002悲观锁
1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...