Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1003;
- int n, m;
- int vis[N][N];
- char c[N][N];
- void dfs(int x, int y) {
- vis[x][y] = 1;
- if (c[x - 1][y] == '.' && vis[x - 1][y] == 0) {
- dfs(x - 1, y);
- }
- if (c[x + 1][y] == '.' && vis[x + 1][y] == 0) {
- dfs(x + 1, y);
- }
- if (c[x][y - 1] == '.' && vis[x][y - 1] == 0) {
- dfs(x, y - 1);
- }
- if (c[x][y + 1] == '.' && vis[x][y + 1] == 0) {
- dfs(x, y + 1);
- }
- }
- int main(){
- cin >> n >> m;
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= m; j++) {
- cin >> c[i][j];
- }
- }
- int ans = 0;
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= m; j++) {
- if (c[i][j] == '.' && vis[i][j] == 0) {
- dfs(i, j);
- ans++;
- }
- }
- }
- cout << ans << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement