Leetcode-1031 Number of Enclaves(飞地的数量)

 1 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 2 #define sz size()
 3 int dx[] = {0,0,1,-1};
 4 int dy[] = {1,-1,0,0};
 5 class Solution
 6 {
 7     public:
 8         void floodfill(vector<vector<int>>& A,int i,int j)
 9         {
10             A[i][j] = 0;
11             _for(k,0,4) 
12             {
13                 int nx = i+dx[k];
14                 int ny = j+dy[k];
15                 if(nx>=0&&ny>=0&&nx<A.size()&&ny<A[nx].size() && A[nx][ny]==1)
16                     floodfill(A,nx,ny);
17             }
18         }
19         int numEnclaves(vector<vector<int>>& A)
20         {
21             _for(i,0,A.sz)
22                _for(j,0,A[i].sz)
23                     if(A[i][j]==1 && (i==0||i==A.size()-1||j==0||j==A[i].size()-1))
24                         floodfill(A,i,j);
25 
26             int res = 0;
27             _for(i,0,A.sz)
28                _for(j,0,A[i].sz)
29                     if(A[i][j]==1)
30                         res ++;
31             return res;
32         }
33 };

flood fill

posted @ 2019-03-31 12:19  Asurudo  阅读(305)  评论(0编辑  收藏  举报