# 宽度优先搜索

LeetCode 200

class Solution {
public:
void DFS(vector<vector<char>>& grid, int i, int j)
{
if(i<0||j<0||i>=grid.size()||j>=grid[0].size()||grid[i][j]!='1') return;
grid[i][j] = '2';
DFS(grid, i+1, j);
DFS(grid, i-1, j);
DFS(grid, i, j+1);
DFS(grid, i, j-1);
}

int numIslands(vector<vector<char>>& grid) {
if(grid.size()==0) return 0;
int cnt = 0, m = grid.size(), n = grid[0].size();
for(int i = 0; i < m; i++)
for(int j =0; j < n; j++)
if(grid[i][j] == '1')
{
cnt++;
DFS(grid, i, j);
}
return cnt;
}
};

class Solution {
public:
int find(int k, vector<int>& u){
return k == u[k] ? k : u[k] = find(u[k],u);
}
int numIslands(vector<vector<char>>& grid) {
int res = 0;
int m = grid.size();
if (!m) return 0;
int n = grid[0].size();
if (!n) return 0;
vector<int>us(grid.size()*grid[0].size());
//从右下角开始遍历
for (int i = m - 1; i >= 0; i--)
for (int j = n - 1; j >= 0; j--){
if (grid[i][j] != '0'){
int k = i*n + j;
//并查集
int s = i != m - 1 && grid[i+1][j] != '0' ? find(k + n, us) : 0;
int t = j != n - 1 && grid[i][j+1] != '0' ? find(k + 1, us) : 0;
if (s == t){
if (s) us[k] = s;
else { us[k] = k; res++; }
}
else{
if (!s) us[k] = t;
else if (!t) us[k] = s;
else{ us[k] = us[s] = us[t] = max(us[s], us[t]); res--; }
}
}
}
return res;
}
};  

01-31

02-22 44
10-23 216
12-03 44
03-03 586
10-22
05-16
10-18 1036
12-06
05-26
06-13
04-17
04-29
06-09