LintCode "Number of Islands II"

A typical Union-Find one. I‘m using a kinda Union-Find solution here. Some boiler-plate code - yeah I know.

class Solution {
    unordered_set<int> hs; // start from 1
    int max_k;
public:
    void dfs(vector<vector<int>> &mt, int x, int y, int f)
    {
    int n = mt.size(), m = mt[0].size();
    mt[y][x] = f;
    if(x > 0 && mt[y][x - 1] != f && mt[y][x - 1] != 0) // left
    {
        mt[y][x - 1] = f;
        dfs(mt, x - 1, y, f);
    }
    if(x < m - 1 && mt[y][x + 1] != f && mt[y][x + 1] != 0) // right
    {
        mt[y][x + 1] = f;
        dfs(mt, x + 1, y, f);
    }
    if(y > 0 && mt[y - 1][x] != f && mt[y - 1][x] != 0) // up
    {
        mt[y - 1][x] = f;
        dfs(mt, x, y - 1, f);
    }
    if(y < n - 1 && mt[y + 1][x] != f && mt[y + 1][x] != 0) // down
    {
        mt[y + 1][x] = f;
        dfs(mt, x, y + 1, f);
    }
    }
    void update(vector<vector<int>> &mt, Point &p)
    {
    int n = mt.size(), m = mt[0].size();

    int minf = INT_MAX;
    vector<Point> to_union;
    if(p.x > 0) // left
    {
        if(mt[p.y][p.x - 1] != 0)
        {
        minf = min(minf, mt[p.y][p.x - 1]);
        to_union.push_back(Point(p.x - 1, p.y));
        }
    }
    if(p.x < m - 1) // right
    {
        if(mt[p.y][p.x + 1] != 0)
        {
        minf = min(minf, mt[p.y][p.x + 1]);
        to_union.push_back(Point(p.x + 1, p.y));
        }
    }
    if(p.y > 0) // update
    {
        if(mt[p.y - 1][p.x] != 0)
        {
        minf = min(minf, mt[p.y - 1][p.x]);
        to_union.push_back(Point(p.x, p.y - 1));
        }
    }
    if(p.y < n - 1) // down
    {
        if(mt[p.y + 1][p.x] != 0)
        {
        minf = min(minf, mt[p.y + 1][p.x]);
        to_union.push_back(Point(p.x, p.y + 1));
        }
    }
    ////
    if(minf == INT_MAX)
    {
        int np = max_k ++;
        mt[p.y][p.x] = np;
        hs.insert(np);
    }
    else
    {
        mt[p.y][p.x] = minf;
        for(auto &tp:to_union)
        {
            if(mt[tp.y][tp.x] != 0 && mt[tp.y][tp.x] != minf)
            {
                int old = mt[tp.y][tp.x];
                dfs(mt, tp.x, tp.y, minf);
                hs.erase(old);
            }
        }
    }
    }
    /**
     * @param n an integer
     * @param m an integer
     * @param operators an array of point
     * @return an integer array
     */
    vector<int> numIslands2(int n, int m, vector<Point>& operators){
        vector<vector<int>> mt(m, vector<int>(n));
        max_k = 1;
        vector<int> ret;
        for(auto &p : operators)
        {
            update(mt, p);
            ret.push_back(hs.size());
        }
        return ret;
    }
};

时间: 2024-10-29 17:04:57

LintCode "Number of Islands II"的相关文章

305.Number of Islands II

/* * 305.Number of Islands II * 2016-4-3 by Mingyang *Union Find 的题目,直接略过 */ private int[][] dir = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; public List<Integer> numIslands2(int m, int n, int[][] positions) { UnionFind2D islands = new UnionFind2D(m, n); L

Number of Islands II

Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i].x, A[i].y

[LeetCode] Number of Islands II

Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands

第九篇 LeetCode Number of Islands II

UnifonFind with path compression and weighting O(klogmn), k is the length of positions array public class Solution { public List<Integer> numIslands2(int m, int n, int[][] positions) { List<Integer> result = new ArrayList<Integer>(); if

[LeetCode] Number of Islands II 岛屿的数量之二

A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand o

[LintCode] Number of Islands 岛屿的数量

Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent. Have you met this ques

Leetcode 305. Number of Islands II

Problem: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each

第八篇 LeetCode Number of Islands

好久没更新了,终于考完了继续回来刷题 岛屿问题属于最基本的DFS, BFS题目 使用DFS时会遇到如果图太大call stack过深的follow up,此时可以转为使用BFS 下一篇Number of Islands II 中将使用另一种Union Find的做法 public class Solution { public int numIslands(char[][] grid) { if (grid == null || grid.length == 0) { return 0; } i

[LeetCode] 200. Number of Islands 岛屿的数量

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