Leetcode-1031 Number of Enclaves(飞地的数量)
Posted asurudo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode-1031 Number of Enclaves(飞地的数量)相关的知识,希望对你有一定的参考价值。
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(飞地的数量)的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] 1031. Maximum Sum of Two Non-Overlapping Subarrays
0434. Number of Segments in a String (E)
CodeForces-1151E-Number of Components