leecode 解题总结:200. Number of Islands

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <unordered_map>
#include <sstream>
using namespace std;
/*
问题:
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:

11110
11010
11000
00000
Answer: 1

Example 2:

11000
11000
00100
00011
Answer: 3

分析:这个实际上就是求被水(0)分割的1的块数,明显可以用深度优先搜索,凡是搜索过的1都标记为访问过的,
下一次搜索的时候随机选择没有被访问过的,继续搜索,island的个数就是搜索的次数

如果用广度优先搜索,也是可以的

输入:
4(行数) 5(列数)
1 1 1 1 0
1 1 0 1 0
1 1 0 0 0
0 0 0 0 0

4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
输出:
1
3

*/

int g_next[][2] = {
	{-1,0},//向左
	{1,0},//向右
	{0,1},//向上
	{0,-1}//向下
};

class Solution {
public:
	//深度优先搜索
	void dfs(int row , int col , int rowMax , int colMax , unordered_map<string ,bool >& visited , 
		vector<vector<char>>& grid)
	{
		string key;
		for(int i = 0 ; i < 4 ; i++)
		{
			int newRow = row + g_next[i][0];
			int newCol = col + g_next[i][1];
			//判定新的位置是否没有超出范围
			if(newRow < 0 || newRow >= rowMax || newCol < 0 || newCol >= colMax )
			{
				continue;
			}
			//如果已经访问过了,跳过
			stringstream stream;
			stream << newRow << "#" << newCol;
			key = stream.str();
			if(visited.find(key) != visited.end())
			{
				continue;
			}
			//如果碰到"0"表示碰到了水,后面无法访问,跳过
			if('0' == grid.at(newRow).at(newCol))
			{
				continue;
			}
			//设置当前结点已经访问
			visited[key] = true;
			//递归访问
			dfs(newRow , newCol , rowMax , colMax , visited , grid);
		}
	}

    int numIslands(vector<vector<char>>& grid) {      
		if(grid.empty())
		{
			return 0;
		}
		int row = grid.size();
		int col = grid.at(0).size();
		unordered_map<string , bool> visited;
		string key;
		int count = 0;
		for(int i = 0 ; i < row ; i++)
		{
			for(int j = 0 ; j < col ; j++)
			{
				//如果是陆地才访问,并且该陆地没有访问过
				if('1' == grid.at(i).at(j) )
				{
					stringstream stream;
					stream << i << "#" << j;
					key = stream.str();
					//如果没有访问过,才访问
					if(visited.find(key) == visited.end())
					{
						count++;
						dfs(i , j , row , col , visited , grid);
					}
				}
			}
		}
		return count;
    }
};

void print(vector<int>& result)
{
	if(result.empty())
	{
		cout << "no result" << endl;
		return;
	}
	int size = result.size();
	for(int i = 0 ; i < size ; i++)
	{
		cout << result.at(i) << " " ;
	}
	cout << endl;
}

void process()
{
	 vector<vector<char>> nums;
	 char value;
	 int row;
	 int col;
	 Solution solution;
	 int result;
	 while(cin >> row >> col )
	 {
		 nums.clear();
		 for(int i = 0 ; i < row ; i++)
		 {
			 vector<char> datas;
			 for(int j = 0 ; j < col ; j++)
			 {
				 cin >> value;
				 datas.push_back(value);
			 }
			 nums.push_back(datas);
		 }
		 result = solution.numIslands(nums);
		 cout << result << endl;
	 }
}

int main(int argc , char* argv[])
{
	process();
	getchar();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值