leetcode200--c++解答(dfs+bfs+并查集)

7 篇文章 0 订阅

题目

给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。

岛屿总是被水包围,并且每座岛屿只能由水平方向或竖直方向上相邻的陆地连接形成。

此外,你可以假设该网格的四条边均被水包围。

示例 1:
输入:

11110
11010
11000
00000

输出: 1

示例 2:
输入:

11000
11000
00100
00011

输出: 3
解释: 每座岛屿只能由水平和/或竖直方向上相邻的陆地连接而成。

解答1:深度优先遍历(dfs):将相连的岛屿均设为0, 求最后留下的1的数量

#include<iostream>
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
		int nr = grid,size();
		if (!nr)
			return 0;
		int nc = grid[0].size();
		if (!nc)
			return 0;
		int nums_islands = 0;
		for (int i = 0; i < nr; ++i) {
			for (int j = 0; j < nc; ++j) {
				if (grid[i][j] == '1') {
					++nums_islands;
					dfs(grid, i, j);
				}
			}
		}
		return nums_islands;
	}
	void dfs(vector<vector<char>> & grid, int i, int j) {
		int nr = grid.size();
		int nc = grid[0].size();
		grid[i][j] = '0';
		if (i - 1 >= 0 && grid[i - 1][j] == '1') dfs(grid, i - 1, j);
		if (i + 1 < nr && grid[i + 1][j] == '1') dfs(grid, i + 1, j);
		if (ij - 1 >= 0 && grid[i][j - 1] == '1') dfs(grid, i, j - 1);
		if (i + 1 < nc && grid[i][j + 1] == '1') dfs(grid, i - 1, j);
	}
};

广度优先遍历(bfs):本质上和dfs类似,就是碰到相邻的岛屿均要设为0

#include<iostream>
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
		int nr = grid,size();
		if (!nr) return 0;
		int nc = grid[0].size();
		if (!nc) return 0;
		int nums_islands = 0;
		for (int i = 0; i < nr; ++i) {
			for (int j = 0; j < nc; ++j) {
				if (grid[i][j] == '1') {
					++nums_islands;
					queue<pair<int, int>> q;
					q.push({i, j});
					while(!q.empty()) {
						auto p = queue.front();
						q.pop();
						int row = p.first, col = p.second;
						if (row - 1 >= 0 && grid[row - 1][col] == '1') {
							p.push({row - 1, col});
							grid[row - 1][col] = '0';
						}
						if (row + 1 < nr && grid[row + 1][col] == '1') {
							p.push({row + 1, col});
							grid[row + 1][col] = '0';
						}
						if (col - 1 >= 0 && grid[row][col - 1] == '1') {
							p.push({row, col - 1});
							grid[row][col - 1] = '0';
						}
						if (col + 1 < nc && grid[row][col + 1] == '1') {
							p.push({row, col + 1});
							grid[row][col + 1] = '0';
						}
					}
				}
			}
		}
		return nums_islands;
	}
};

并查集:基于rank的优化,相邻的岛屿的根相同

#include<iostream>
class UnionFind{
public:
	UnionFind(vector<vector<char>> grid) {
		count = 0;
		int nr = grid.size();
		int nc = grid[0].size();
		for (int i = 0; i < nr; ++i) {
			for (int j = 0; j < nc; ++j) {
				if (grid[i][j] == '1') {
					parent.push_back(i * n + j);
					++count;
				} else {
					parent.push_back(-1);
				}
				rank.push_back(0);
		}
	}
	   int find(int i) {
      	 if (parent[i] != i) {
            parent[i] = find(parent[i]);
        }
        return parent[i];
    }
    void union(int x, int y) {
		int rootx = find(x);
		int rooty = find(y);
		if (rootx != rooty) {
			if (rank(x) < rank(y)) {
				swap(rootx, rooty);
			}
			parent[rooty] = rootx;
			if (rank[rootx] == rank[rooty]) rank[rootx] += 1;
            --count;
		}
	}
	int getCount() const {
        return count;
    }
}
private:
	vector<int> parent;
	vector<int> rank;
	int count;
}
class Solution {
public:
	    int numIslands(vector<vector<char>>& grid) {
		int nr = grid,size();
		if (!nr) return 0;
		int nc = grid[0].size();
		if (!nc) return 0;
		UnionFind uf(grid);
        int num_islands = 0;
        for (int r = 0; r < nr; ++r) {
            for (int c = 0; c < nc; ++c) {
                if (grid[r][c] == '1') {
                    grid[r][c] = '0';
                    if (r - 1 >= 0 && grid[r-1][c] == '1') uf.unite(r * nc + c, (r-1) * nc + c);
                    if (r + 1 < nr && grid[r+1][c] == '1') uf.unite(r * nc + c, (r+1) * nc + c);
                    if (c - 1 >= 0 && grid[r][c-1] == '1') uf.unite(r * nc + c, r * nc + c - 1);
                    if (c + 1 < nc && grid[r][c+1] == '1') uf.unite(r * nc + c, r * nc + c + 1);
                }
            }
        }
        return uf.getCount();
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值