LeetCode solution summary 1254. Count the number of closed islands

Directory link:

Lituo Programming Problems - Summary of Solutions_Share+Records-CSDN Blog

GitHub synchronous brushing project:

https://github.com/September26/java-algorithms

Link to the original title: Likou


describe:

A two-dimensional matrix  consisting grid of  0 (land) and  1 (water). 0 An island is a group composed  of the largest 4 directions connected  , and a closed island is an 完全 island surrounded by 1 (left, upper, right, lower).

Please return  the number of closed islands  .

Example 1:

Input: grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0, 1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
 Output: 2
 Explanation: 
Islands in the gray area are closed islands because the island is completely surrounded by water (ie surrounded by 1 area).

Example 2:

Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
 Output: 1

Example 3:

Input: grid = [[1,1,1,1,1,1,1], 
             [1,0,0,0,0,0,1], 
             [1,0,1,1,1,0, 1], 
             [1,0,1,0,1,0,1], 
             [1,0,1,1,1,0,1], 
             [1,0,0,0,0,0,1] , 
             [1,1,1,1,1,1,1]] output: 2

hint:

  • 1 <= grid.length, grid[0].length <= 100
  • 0 <= grid[i][j] <=1

 

Problem-solving ideas:

/**
 * 1254. 统计封闭岛屿的数目
 * 解题思路:
 * 标记状态,0代表没有遍历,1代表海域,2代表遍历中,3代表是不是独立岛屿,4代表是独立岛屿。
 * 然后遍历grid,如果grid[i][j]==0,则查找从这个点触发所有能达到的区域,并记录。返回值是是否是独立岛屿,
 * 如果是则把所有达到的点改为4,并且数量+1,否则改为3。
 * 然后继续查找下一个不为0的点。
 */

code:

class Solution {
public:
    vector<vector<int>> directions = {
        {1, 0},
        {0, 1},
        {-1, 0},
        {0, -1},
    };
    const int STATE_NO_TRAVEL = 0;       // 没有遍历
    const int STATE_SEA = 1;             // 海域
    const int STATE_SEARCHING = 2;       // 遍历中
    const int STATE_NO_CLOSE_ISLAND = 3; // 确定不是独立岛屿
    const int STATE_CLOSE_ISLAND = 4;    // 确定是独立岛屿

    bool searchClosedIsland(vector<vector<int>> &grid, bool parentFlag, int x, int y, vector<vector<int>> &record)
    {
        if (y == 0 || y == grid.size() - 1 || x == 0 || x == grid[0].size() - 1)
        {
            record.push_back({y, x});
            return false;
        }
        record.push_back({y, x});
        bool flag = true;
        for (int i = 0; i < directions.size(); i++)
        {
            int newX = x + directions[i][1];
            int newY = y + directions[i][0];
            // 为3代表正在遍历中
            if (grid[newY][newX] == STATE_SEARCHING)
            {
                continue;
            }
            // 为1代表遇到海水
            if (grid[newY][newX] == STATE_SEA)
            {
                continue;
            }
            // 为2代表遇到未封闭的岛屿
            if (grid[newY][newX] == STATE_NO_CLOSE_ISLAND)
            {
                flag = false;
                continue;
            }
            // 为0代表未遍历过
            if (grid[newY][newX] != 0)
            {
                cout << "error" << endl;
            }
            grid[newY][newX] = STATE_SEARCHING;
            flag = flag & searchClosedIsland(grid, flag, newX, newY, record);
        }
        return flag & parentFlag;
    }

    int closedIsland(vector<vector<int>> &grid)
    {
        int sum = 0;
        vector<vector<int>> record;
        for (int y = 0; y < grid.size(); y++)
        {
            for (int x = 0; x < grid[0].size(); x++)
            {
                if (grid[y][x] != 0)
                {
                    continue;
                }
                bool flag = searchClosedIsland(grid, true, x, y, record);
                if (flag)
                {
                    sum++;
                }
                for (auto it : record)
                {
                    // cout << it[0] << " ";
                    grid[it[0]][it[1]] = flag ? STATE_CLOSE_ISLAND : STATE_NO_CLOSE_ISLAND;
                }
                record.clear();
            }
        }
        return sum;
    }
};

 

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/131314031