Leetcode 200. Number of Island

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014303647/article/details/88727014

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:

Input:
11110
11010
11000
00000

Output: 1
Example 2:

Input:
11000
11000
00100
00011

Output: 3

题意:就是给一个二维数组,然后看有多少个连着的小岛,其实本质就是在找联通分量。

我写了两种解法:

第一种:是用dfs的做法,就只找联通分量。只需要一个数组即可。

代码:

#include <iostream>
#include <vector>
using namespace std;

/*
    搜素做dfs
*/

class Solution
{
public:

	int dx[4] = {-1,1,0,0};
	int dy[4] = {0,0,1,-1};

	bool Judge(int x,int y,int row,int col)
	{
	    if(x<0||x>row||y<0||y>col) return false;
	    return true;
	}

	void dfs(int x, int y, vector<vector<bool>>&vis,vector<vector<char>>& grid,int row,int col)
	{
		vis[x][y] = true;
		for(int i = 0; i < 4 ; i++)
		{
			int xx = x + dx[i];
			int yy = y + dy[i];
			if(Judge(xx,yy,row,col) && !vis[xx][yy] && grid[xx][yy] == '1')  dfs(xx,yy,vis,grid,row,col);
		}
	}
    int numIslands(vector<vector<char>>& grid)
    {
    	int cnt = 0;
    	int row = grid.size();
    	if(row == 0) return 0;
    	int col = grid[0].size();
    	if(col==0) return 0;
    	vector<vector<bool>> vis;
    	vis.resize(row);
    	for(int i = 0; i<row; i++) vis[i].resize(col);
    	for(int i = 0; i<row; i++)
    		for(int j = 0;j<col;j++)
    			vis[i][j] = false;
    	for(int i = 0; i<row;i++)
    	{
    		for(int j = 0; j <col; j++)
    		{
    			if(grid[i][j]=='1' && !vis[i][j])
    			{
    				cnt++;
    				dfs(i,j,vis,grid,row-1,col-1);
    			}
    		}
    	}
    	return cnt;

    }
};

int main()
{
    initializer_list<vector<char>> lst = {
        {'1','1','0','0','0'},
        {'1','1','0','0','0'},
        {'0','0','1','0','0'},
        {'0','0','0','1','1'}
    };
    Solution so;
    vector<vector<char>> grid(lst);
    cout << so.numIslands(grid);
    return 0;
}


第二种方法是并查集。

就是把处于一个连接块的岛屿放在一个连接分量里,即连着一个共同祖先。

代码:

class Solution
{
public:

	int findroot(int x, int root[])
	{
		if (root[x] != x)
			root[x] = findroot(root[x], root);
		return root[x];
	}

	bool Judge(int x, int y, int row, int col)
	{
		if (x<0 || x>row || y<0 || y>col) return false;
		return true;
	}

	int dx [4] = { -1, 1, 0, 0 };
	int dy [4] = { 0, 0, -1, 1 };

	int numIslands(vector<vector<char>>& grid)
	{
		int row = grid.size();
		if (row == 0) return 0;
		int col = grid[0].size();
		if (col == 0) return 0;
		int root[row*col+10];
		for (int i = 0; i < row * col + 10; i++)  root[i] = i;
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				int x_value = i * col + j;
				if (grid[i][j] == '0')  continue;
				for (int k = 0; k < 4; k++)
				{
					int xx = i + dx[k];
					int yy = j + dy[k];
					if (Judge(xx, yy, row - 1, col - 1) && grid[xx][yy] == '1')
					{
						int y_value = xx * col + yy;
						int fa = findroot(x_value, root);
						int fb = findroot(y_value, root);
						if (fa != fb) root[fa] = fb;
					}
				}
			}
		}
		int ans = 0;
		for (int i = 0; i< row; i++)
			for (int j = 0; j< col; j++)
			{
				int x_value = i * col + j;
				if (root[x_value] == x_value && grid[i][j] == '1') ++ans;
			}
		return ans;
	}
};

猜你喜欢

转载自blog.csdn.net/u014303647/article/details/88727014