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 ...
随机推荐
- 开启VMware Esxi的SSH远程登录
1.在服务器的配置页面中开启 按[F2],输入密码,进入配置页面,选择troubleshooting options,选择EnableSSH 即可. 2.在VMware Client中开启 进入:配置 ...
- NC保存报dirty解决方法
在NC UI端测试保存的时候报 " The data whose initcode is 6033 is dirty! " 错误,其解决方式是 在IHrPf接口中添加你的单据模板编号 和 参数模板 ...
- 11个实用jQuery日历插件
1. FullCalendar FullCalendar是很出名的jQuery日历插件,它支持拖拽等功能,整合了Google Calendar,而且可以通过JSON来绑定事件,设计师可以轻松地自定义日 ...
- SPOJ LCS2 后缀自动机
多串的LCS,注意要利用拓扑序更新suf的len. 我用min,max,三目会超时,所以都改成了if,else #pragma warning(disable:4996) #include<cs ...
- MVC中SelectList和@Html.DropDownList("MainDuty_UserId","请选择")的运用
Models.Project model = projectdb.dbSet.SingleOrDefault(e => e.Project_ID == id); ViewB ...
- C#中 ? 和?? 的用法
C#中 ?? 和? 的意思 1.? 如果直接定义一个 值类型,给负值null:就会提示“无法将 Null转换成‘值类型(比如:int)’,因为他是一种不可为null的值 de类型” 例如 int in ...
- javascript背景淡入淡出
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Spring 3.0: Unable to locate Spring NamespaceHandler for XML schema namespace
被这个问题折磨着很久:参考: http://have23.iteye.com/blog/1340777 (cfx 与 spring 整合的时候出现的问题: org.springframework.be ...
- 汇编debug 截图
- seafile安装日志(非教程)
需要的软件: python 2.7.x(从 Seafile 5.1 开始,python 版本最低要求为2.7) python-setuptools python-imaging python-mysq ...