diff --git a/Daily/Number_Of_Enclaves.cpp b/Daily/Number_Of_Enclaves.cpp new file mode 100644 index 00000000..b290bc38 --- /dev/null +++ b/Daily/Number_Of_Enclaves.cpp @@ -0,0 +1,122 @@ +/* +You are given an n x m binary matrix grid, where 0 represents a sea cell and 1 represents a land cell. + +A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid. + +Find the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves. + +Example 1: + +Input: +grid[][] = {{0, 0, 0, 0}, + {1, 0, 1, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 0}} +Output: +3 +Explanation: +0 0 0 0 +1 0 1 0 +0 1 1 0 +0 0 0 0 +The highlighted cells represents the land cells. + +Example 2: + +Input: +grid[][] = {{0, 0, 0, 1}, + {0, 1, 1, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 1}, + {0, 1, 1, 0}} +Output: +4 +Explanation: +0 0 0 1 +0 1 1 0 +0 1 1 0 +0 0 0 1 +0 1 1 0 +The highlighted cells represents the land cells.*/ + +//{ Driver Code Starts +// Initial Template for C++ + +#include +using namespace std; + + +// } Driver Code Ends +// User function Template for C++ + +class Solution { + private: + void dfs(int i,int j,vector> &visit,vector> &grid){ + visit[i][j] = 1; + int n = grid.size(); + int m = grid[0].size(); + + int row[] = {1,0,-1,0}; + int col[] = {0,1,0,-1}; + + for(int k=0;k<4;k++){ + int nr = i+row[k]; + int nc = j+col[k]; + + if(nr<0||nr>=n||nc<0||nc>=m) continue; + + if(grid[nr][nc]==1 && !visit[nr][nc]){ + visit[nr][nc] = 1; + dfs(nr,nc,visit,grid); + } + } + } + public: + int numberOfEnclaves(vector> &grid) { + // Code here + + int n = grid.size(); + int m = grid[0].size(); + + vector> visit(n,vector(m,0)); + + for(int i=0;i> t; + while (t--) { + int n, m; + cin >> n >> m; + vector> grid(n, vector(m)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> grid[i][j]; + } + } + Solution obj; + cout << obj.numberOfEnclaves(grid) << endl; + } +} +// } Driver Code Ends \ No newline at end of file