Address https://leetcode-cn.com/contest/weekly-contest-162/problems/number-of-closed-islands/
There is a two-dimensional matrix grid
, each location either on land (to mark 0
) is either water (for the sign 1
).
We proceed from one land mass, each can up under the direction of the adjacent area of about four to go, and went to all land areas, which we will call an "island."
If an island completely surrounded by water, that is, all the adjacent land areas around the edge of the waters are up and down, then we will call it "closed the island."
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:
island island gray area is closed, because the island is completely surrounded by water (i.e., an area enclosed).
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
The basic solution to a problem is to find out if using DFS has communicated with 0 border
It provides another set of ideas here and check
All markers for the detection merge land 0 land boundary and then communicated to delete the last remaining set of islands is closed
Code
1 class Solution { 2 public: 3 4 int MAX_NUM; 5 vector<vector<int>> field; 6 vector<int> fa; 7 vector<int> addx; 8 vector<int> addy; 9 void init(int n) 10 { 11 for (int i = 0; i <= n; i++) 12 fa[i] = i; 13 } 14 int GET ( int X) 15 { 16 return FA [X] X X ==:? FA [X] = GET (FA [X]); // path compressor, to prevent chain structure . 17 } 18 is void Merge ( int X, int Y) . 19 { 20 is FA [ GET (X)] = GET (Y); 21 is } 22 is // ======================= ========================= 23 is void Check ( int X, int Y, Vector <Vector < int >> & grid) 24 { 25 for (int i = 0; i < 4; i++) { 26 int newx = x + addx[i]; 27 int newy = y + addy[i]; 28 29 if (newx >= 0 && newx < grid.size() && newy >= 0 && newy < grid[0].size() 30 && grid[newx][newy] == 0) 31 { 32 int idx = x * grid[0].size() + y; 33 int anotherIdx = newx * grid[0].size() + newy; 34 merge(idx, anotherIdx); 35 } 36 } 37 } 38 39 int closedIsland(vector<vector<int>>& grid) { 40 MAX_NUM = 110; 41 field = vector<vector<int>>(MAX_NUM, vector<int>(MAX_NUM)); 42 fa = vector<int>(MAX_NUM*MAX_NUM + 1); 43 init(MAX_NUM*MAX_NUM); 44 addx = vector<int>{ 1,-1,0,0 }; 45 addy = vector<int>{ 0,0,-1,1 }; 46 for (int i = 0; i < grid.size(); i++) { 47 for (int j = 0; j < grid[0].size(); j++) { 48 if (grid[i][j] == 0) { 49 check(i, j, grid); 50 } 51 } 52 } 53 54 set<int> s; 55 56 for (int i = 0; i < grid.size(); i++) { 57 for (int j = 0; j < grid[0].size(); j++) { 58 if (grid[i][j] == 0) { 59 int idx = i * grid[0].size() + j; 60 s.insert(get(idx)); 61 } 62 } 63 } 64 65 // from disjoint-set of statistics relating to remove edge land 66 for ( int I = 0 ; I <grid.size (); I ++ ) { 67 for ( int J = 0 ; J <Grid [ 0 ] .size (); J ++ ) { 68 IF (Grid [I] [J] == 0 && (I == 0 || I grid.size == () - . 1 || J == 0 Grid J == || [ 0 ] .size () - . 1 )) { 69 int idx = i * grid[0].size() + j; 70 s.erase(get(idx)); 71 } 72 } 73 } 74 75 return s.size(); 76 } 77 78 };