Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int dx[] = {1, 0, -1, 0};
- int dy[] = {0, 1, 0, -1};
- int n, m;
- int vis[1005][1005], a[1005][1005];
- bool isValid(int x, int y) {
- if(x < 0 || y < 0 || x >= n || y >= m || a[x][y] || vis[x][y])
- return false;
- return true;
- }
- void dfs(int x, int y) {
- if(isValid(x, y)) {
- vis[x][y] = 1;
- for(int i = 0; i < 4; i++) {
- dfs(x + dx[i], y + dy[i]);
- }
- }
- return;
- }
- int main() {
- ios::sync_with_stdio(0); cin.tie(0);
- memset(vis, 0, sizeof(vis));
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- string s;
- cin >> s;
- for(int j = 0; j < s.length(); j++)
- a[i][j] = (s[j] == '#');
- }
- int ans = 0;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- if(a[i][j] == 0 && !vis[i][j]) {
- dfs(i, j);
- ans++;
- }
- }
- }
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement