leetcode 200——岛屿数量

leetcode 200——岛屿数量(C++提交)

题目链接:leetcode 200——岛屿数量

题目描述:

给定一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

示例 1:

输入:
11110
11010
11000
00000

输出: 1
示例 2:

输入:
11000
11000
00100
00011

输出: 3

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-islands
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

提交:递归+dfs

在这里插入图片描述

//递归 + dfs
class Solution {
public:
	void dfs(vector<vector<char>>& grid, int x, int y)
	{
		grid[x][y] = '0';
		vector<pair<int, int>> directions = { {-1,0},{1,0},{0,-1},{0,1} };//四连通  上下左右四个方向
		for (int i = 0; i < 4; i++)
		{
			int nx = x + directions[i].first, ny = y + directions[i].second;
			if (nx >= 0 && ny >= 0 && nx < grid.size() && ny < grid[0].size() && grid[nx][ny] == '1') dfs(grid, nx, ny);
		}
		return;
	}
    
    int numIslands(vector<vector<char>>& grid) {
        int count = 0;
        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[0].size();j++)
            {
                if(grid[i][j] == '1')
                {
                    dfs(grid,i,j);
                    count++;
                }
            }
        }
        return count;
    }
};

提交:递归+dfs优化:

在这里插入图片描述

class Solution {
public:
	void dfs(vector<vector<char>>& grid, int x, int y)//感染函数,一次完整的感染后,将四连通的岛屿全变为'0'
	{
		int row = grid.size();//地图的行数
		int cols = grid[0].size();//地图的列数

		grid[x][y] = '0';
		if (x - 1 >= 0 && grid[x - 1][y] == '1') dfs(grid, x - 1, y);
		if (x + 1 < row && grid[x + 1][y] == '1') dfs(grid, x + 1, y);
		if (y - 1 >= 0 && grid[x][y - 1] == '1') dfs(grid, x, y - 1);
		if (y + 1 < cols && grid[x][y + 1] == '1') dfs(grid, x, y + 1);
	/*
		//这样也可:
		if(x>=0&&y>=0&&x<grid.size()&&y<grid[0].size()&&grid[x][y]=='1')
		{
			grid[x][y] = '0';
			dfs(grid,x-1,y);
			dfs(grid,x+1,y);
			dfs(grid,x,y-1);
			dfs(grid,x,y+1);
		}
		
	*/
	}

	int numIslands(vector<vector<char>>& grid) {
		//疑惑???????????
		//为什么这里用row和cols接收一下就用不过?????
		//搞不明白了
		//int row = grid.size();//地图的行数
		//int cols = grid[0].size();//地图的列数
		//if (row == 0 || cols == 0) return 0;//地图为空时岛屿数量为零

		int num_islands = 0;//岛屿数量初始化
		//从左上角进行寻找 '1',寻找后对其进行感染(dfs的递归过程)。经过一次完整的感染后,岛屿数量增加1。 
		for (int i = 0; i < grid.size(); i++)
		{
			for (int j = 0; j < grid[0].size(); j++)
			{
				if (grid[i][j] == '1')
				{
					dfs(grid, i, j);
					num_islands++;
				}
			}
		}
		return num_islands;
	}
};

提交:bfs (不知道为什么通不过…………)

class Solution {
public:
	int numIslands(vector<vector<char>>& grid) {
		int r_num = grid.size();		//行数
		int c_num = grid[0].size();		//列数
		if (r_num == 0 || c_num == 0) return 0; //判断入口
		int islands_num = 0;			//岛屿个数

		for (int r = 0; r < r_num; r++)
		{
			for (int c = 0; c < c_num; c++)
			{
				if (grid[r][c] == '1')//以每个岛屿的首个'1'进行广度扩散。
				{
					islands_num++;
					//入队
					queue<pair<int, int>> q;
					q.push(pair<int, int>(r, c));
					grid[r][c] = '0';
					while (!q.empty())
					{
						//出队
						pair<int, int> tempPoint = q.front(); q.pop();
						int row = tempPoint.first, cols = tempPoint.second;
						//广度的扩散过程:
						if (row - 1 >= 0 && grid[row - 1][cols] == '1')
						{
							q.push(pair<int, int>(row - 1, cols));
							grid[row - 1][cols] = '0';
						}
						if (row + 1 < r_num && grid[row + 1][cols] == '1')
						{
							q.push(pair<int, int>(row + 1, cols));
							grid[row + 1][cols] = '0';
						}
						if (cols - 1 >= 0 && grid[row][cols - 1] == '1')
						{
							q.push(pair<int, int>(row, cols - 1));
							grid[row][cols - 1] = '0';
						}
						if (cols + 1 < c_num && grid[row][cols + 1] == '1')
						{
							q.push(pair<int, int>(row, cols + 1));
							grid[row][cols + 1] = '0';
						}
					}
				}
			}
		}
		return islands_num;
	}
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值