Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n, m;
- cin>>n>>m;
- vector<string> s(n);
- for(auto &e: s) cin>>e;
- vector<vector<int>> vis(n, vector<int> (m, 0));
- int x[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
- int y[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
- function<void(int, int)> DFS = [&](int i, int j)
- {
- vis[i][j] = 1;
- for(int d=0; d<8; d++)
- {
- if(i+x[d]>=0 && i+x[d]<n && j+y[d]>=0 && j+y[d]<m && !vis[i+x[d]][j+y[d]] && s[i+x[d]][j+y[d]]=='#')
- DFS(i+x[d], j+y[d]);
- }
- };
- int ans = 0;
- for(int i=0; i<n; i++)
- {
- for(int j=0; j<m; j++)
- {
- if(!vis[i][j] && s[i][j]=='#')
- {
- ans++;
- DFS(i, j);
- }
- }
- }
- cout<<ans<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment