1. 程式人生 > >Leetcode-1031 Number of Enclaves(飛地的數量)

Leetcode-1031 Number of Enclaves(飛地的數量)

urn lee cto out public tco div ++ res

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

flood fill

Leetcode-1031 Number of Enclaves(飛地的數量)