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
这个题有两种方法:
way-1:左上角开始找点,如果是1,就广度遍历(BFS)或者深度遍历(DFS)这个island的点,全部设为2。
然后ret++;
way-2:使用并查集(union find),其实这种找集合的题使用并查集是最简单的。这两种方法都必须掌握。
way-1代码
struct POS
{
int x;
int y;
POS(int xx, int yy):x(xx), y(yy){}
};
class Solution
{
public:
void BFS(vector<vector<char>>& board, int i, int j) //广度遍历 队列
{
int a[4] = {1, 0, -1, 0};
int b[4] = {0, 1, 0, -1};
queue<POS*> pos;
pos.push(new POS(i,j));
board[i][j] = '2';
while(!pos.empty())
{
POS *front = pos.front();
pos.pop();
for (int i = 0; i < 4; i++)
{
if (isvalid(front->x + a[i], front->y + b[i]) && board[front->x + a[i]][front->y + b[i]] == '1')
{
pos.push(new POS(front->x + a[i], front->y + b[i]));
board[front->x + a[i]][front->y + b[i]] = '2';
}
}
}
}
void DFS(vector<vector<char>>& board, int i, int j) //深度遍历 栈
{
int a[4] = {1, 0, -1, 0};
int b[4] = {0, 1, 0, -1};
stack<POS*> pos;
pos.push(new POS(i, j));
board[i][j] = '2';
bool isfind; //由于在当前点周边找到新的符合要求的点就应该continue,但是在while里面还有一层for,continue无法开始新的while,所以需要一个isfind到外面去continue
while (!pos.empty())
{
POS *front = pos.top();
isfind = false;
for (int i = 0; i < 4; i++)
{
if (isvalid(front->x + a[i], front->y + b[i]) && board[front->x + a[i]][front->y + b[i]] == '1')
{
pos.push(new POS(front->x + a[i], front->y + b[i]));
board[front->x + a[i]][front->y + b[i]] = '2';
isfind = true;
break;
}
}
if (isfind)
continue;
pos.pop();
}
}
bool isvalid(int i, int j)
{
return i >= 0 && i < row && j >=0 && j < col;
}
int numIslands(vector<vector<char>>& grid)
{
int ret = 0;
row = grid.size();
if (row == 0)
return ret;
col = grid[0].size();
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (grid[i][j] == '1')
{
ret++;
//BFS(grid, i, j);
DFS(grid, i, j);
}
}
}
return ret;
}
private:
int row;
int col;
};
class Solution
{
public:
int numIslands(vector<vector<char>>& grid)
{
row = grid.size();
if (row == 0) return 0;
col = grid[0].size();
for (int i = 0; i < row * col; i++)
father.push_back(i);
int island = 0;
int a[4] = {1, 0, -1, 0};
int b[4] = {0, 1, 0, -1};
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (grid[i][j] == '1')
{
island++;
for (int k = 0; k < 4; k++)
{
int _x = i + a[k];
int _y = j + b[k];
if (isvalid(_x, _y) && grid[_x][_y] == '1')
{
int father_1 = find_father(i * col + j);
int father_2 = find_father(_x * col + _y);
if (father_1 != father_2)
{
father[father_2] = father_1;
island--;
}
}
}
}
}
}
return island;
}
private:
int row;
int col;
vector<int> father;
int find_father(int x)
{
if (father[x] == x)
return x;
return father[x] = find_father(father[x]);
}
bool isvalid(int i, int j)
{
return i >= 0 && i < row && j >=0 && j < col;
}
};
本文介绍两种高效算法来计算二维网格中由'1'表示的陆地形成的岛屿数量。一种是通过广度优先搜索(BFS)或深度优先搜索(DFS)进行遍历;另一种则是采用并查集(union-find)来解决该问题。
1161

被折叠的 条评论
为什么被折叠?



