Number of Islands
来源:互联网 发布:mac如何建立文件夹 编辑:程序博客网 时间:2024/05/22 15:28
题目名称
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 surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110110101100000000
Answer: 1
Example 2:
11000110000010000011
Answer: 3
分析
这道题就是典型的图的搜索算法,一个小岛定义为它的上下左右四个位置都有水,一个小岛必须是连通的,题目就可以转换为求不带权无向图的连通分量的个数。用BFS和DFS都能够求解问题,从第一个值为’1’的地方开始遍历,先将其置为’0’(相当于图的遍历中设置为已经被访问),每找到一个相邻的土地,都将其设置为’0’,这里附上代码和测试用例。
C++代码
DFS:
用BFS比DFS时间消耗要少一点,因为这里的DFS用的是递归的方法来遍历的。
void DFS(vector<vector<char> > &grid,int x,int y) { grid[x][y] = '0'; if(x>0 && grid[x-1][y]=='1') DFS(grid,x-1,y); if(x<grid.size()-1 && grid[x+1][y]=='1') DFS(grid,x+1,y); if(y>0 && grid[x][y-1]=='1') DFS(grid,x,y-1); if(y<grid[0].size()-1 && grid[x][y+1]=='1') DFS(grid,x,y+1);} int numIslands(vector<vector<char> > &grid) { int res = 0; if(grid.size()==0 || grid[0].size()==0) return res; for(int i=0;i<grid.size();i++) { for(int j=0;j<grid[0].size();j++) { if(grid[i][j]=='1') { ++res; DFS(grid,i,j); } } } return res;}
BFS:
void BFS(vector<vector<char> > &grid,int x,int y) { queue<vector<int> > qu; vector<int> temp(2); temp[0] = x; temp[1] = y; qu.push(temp); grid[x][y] = '0'; while(!qu.empty()) { x = qu.front()[0]; y = qu.front()[1]; qu.pop(); if(x>0 && grid[x-1][y]=='1') { temp[0] = x-1; temp[1] = y; qu.push(temp); grid[x-1][y] = '0'; } if(x < grid.size()-1 && grid[x+1][y] == '1') { temp[0] = x+1; temp[1] = y; qu.push(temp); grid[x + 1][y] = '0'; } if(y > 0 && grid[x][y-1] == '1') { temp[0] = x; temp[1] = y-1; qu.push(temp); grid[x][y-1] = '0'; } if(y < grid[0].size()-1 && grid[x][y+1] == '1') { temp[0] = x; temp[1] = y+1; qu.push(temp); grid[x][y + 1] = '0'; } }}int numIslands(vector<vector<char> > &grid) { int res=0; if(grid.size()==0 || grid[0].size()==0) return res; for(int i=0;i<grid.size();++i) { for(int j=0;j<grid[0].size();++j) { if(grid[i][j]=='1') { ++res; BFS(grid,i,j); } } } return res;}
测试用例
#include<iostream>#include<vector>using namespace std;void DFS(vector<vector<char> > &grid,int x,int y) { grid[x][y] = '0'; if(x>0 && grid[x-1][y]=='1') DFS(grid,x-1,y); if(x<grid.size()-1 && grid[x+1][y]=='1') DFS(grid,x+1,y); if(y>0 && grid[x][y-1]=='1') DFS(grid,x,y-1); if(y<grid[0].size()-1 && grid[x][y+1]=='1') DFS(grid,x,y+1);} int numIslands(vector<vector<char> > &grid) { int res = 0; if(grid.size()==0 || grid[0].size()==0) return res; for(int i=0;i<grid.size();i++) { for(int j=0;j<grid[0].size();j++) { if(grid[i][j]=='1') { ++res; DFS(grid,i,j); } } } return res;}int main() { char a[] = {'1','1','1','1','0'}; char b[] = {'1','1','0','1','0'}; char c[] = {'1','1','0','0','0'}; char d[] = {'0','0','0','0','0'}; vector<vector<char> > grid; vector<char> A(a,a+5); vector<char> B(b,b+5); vector<char> C(c,c+5); vector<char> D(d,d+5); grid.push_back(A); grid.push_back(B); grid.push_back(C); grid.push_back(D); //输出grid cout<<"["<<endl; for(int i=0;i<grid.size();i++) { cout<<"["<<" "; for(int j=0;j<grid[0].size();j++) { cout<<grid[i][j]<<" "; } cout<<"]"<<endl; } cout<<endl; int num = numIslands(grid); cout<<"The number of the islands is:"<<num<<endl; //输出变化后的grid cout<<"["<<endl; for(int i=0;i<grid.size();i++) { cout<<"["<<" "; for(int j=0;j<grid[0].size();j++) { cout<<grid[i][j]<<" "; } cout<<"]"<<endl; } cout<<endl; return 0;}
#include<iostream>#include<vector>#include<queue>using namespace std;void BFS(vector<vector<char> > &grid,int x,int y) { queue<vector<int> > qu; vector<int> temp(2); temp[0] = x; temp[1] = y; qu.push(temp); grid[x][y] = '0'; while(!qu.empty()) { x = qu.front()[0]; y = qu.front()[1]; qu.pop(); if(x>0 && grid[x-1][y]=='1') { temp[0] = x-1; temp[1] = y; qu.push(temp); grid[x-1][y] = '0'; } if(x < grid.size()-1 && grid[x+1][y] == '1') { temp[0] = x+1; temp[1] = y; qu.push(temp); grid[x + 1][y] = '0'; } if(y > 0 && grid[x][y-1] == '1') { temp[0] = x; temp[1] = y-1; qu.push(temp); grid[x][y-1] = '0'; } if(y < grid[0].size()-1 && grid[x][y+1] == '1') { temp[0] = x; temp[1] = y+1; qu.push(temp); grid[x][y + 1] = '0'; } }}int numIslands(vector<vector<char> > &grid) { int res=0; if(grid.size()==0 || grid[0].size()==0) return res; for(int i=0;i<grid.size();++i) { for(int j=0;j<grid[0].size();++j) { if(grid[i][j]=='1') { ++res; BFS(grid,i,j); } } } return res;}int main() { char a[] = {'1','1','1','1','0'}; char b[] = {'1','1','0','1','0'}; char c[] = {'1','1','0','0','0'}; char d[] = {'0','0','0','0','0'}; vector<vector<char> > grid; vector<char> A(a,a+5); vector<char> B(b,b+5); vector<char> C(c,c+5); vector<char> D(d,d+5); grid.push_back(A); grid.push_back(B); grid.push_back(C); grid.push_back(D); //输出grid cout<<"["<<endl; for(int i=0;i<grid.size();i++) { cout<<"["<<" "; for(int j=0;j<grid[0].size();j++) { cout<<grid[i][j]<<" "; } cout<<"]"<<endl; } cout<<endl; int num = numIslands(grid); cout<<"The number of the islands is:"<<num<<endl; //输出变化后的grid cout<<"["<<endl; for(int i=0;i<grid.size();i++) { cout<<"["<<" "; for(int j=0;j<grid[0].size();j++) { cout<<grid[i][j]<<" "; } cout<<"]"<<endl; } cout<<endl; return 0;}
0 0
- Number of Islands
- Number of Islands
- Number of Islands
- [LeetCode] Number of Islands
- Leetcode-Number of Islands
- Number of Islands
- Leetcode Number of Islands
- [LeetCode]Number of Islands
- Number of Islands
- Number of Islands
- [leetcode]Number of Islands
- [leetcode] Number of Islands
- leetcode Number of Islands
- leetcode:Number of Islands
- [leetcode] Number of Islands
- 200 Number of Islands
- [LeetCode] Number of Islands
- Number of Islands
- Leetcode之Gas Station
- UVA 133(82)----The Dole Queue
- 02.C++学习笔记------const关键字
- android Services
- Xcode 如何修改类里面生成的Create By 和组织信息
- Number of Islands
- 整合了刷新、加载更多、滑动删除功能的XListview
- [已解决]TypeError: 'module' object is not callable
- Toolbar-5.0新特性
- 数据挖掘、自然语言处理
- 连接远程桌面
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Recreating an Activity) (四)
- java之断点调试
- md5-sha 摘要加密算法