示例#1
0
文件: f.cpp 项目: Neverous/ii-aisd12
int &unionFind(const int &a)
{
    if(repr[a] == a)
        return repr[a];

    return repr[a] = unionFind(repr[a]);
}
示例#2
0
inline short int &unionFind(const short int &a)
{
    if(repr[a] != a)
        repr[a] = unionFind(repr[a]);

    return repr[a];
}
示例#3
0
文件: f.cpp 项目: Neverous/ii-aisd12
bool unionUnion(const int &a, const int &b)
{
    int &fa = unionFind(a),
        &fb = unionFind(b);

    if(fa == fb)
        return false;

    if(fa < fb)
        repr[fb] = fa;

    else
        repr[fa] = fb;

    return true;
}
 int numIslands(vector<vector<char>> &grid) {
   if (grid.empty())
     return 0;
   int count = 0, R = grid.size(), C = grid[0].size();
   for (int row = 0; row < R; ++row) {
     for (int column = 0; column < C; ++column) {
       if (grid[row][column] == '1') {
         unionFind(grid, row, column, R, C);
         ++count;
       }
     }
   }
   return count;
 }