Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int a,b;
- void dfs(int i, int j , vector<vector<int>>& vis, vector<vector<char>> y) {
- vis[i][j] = 1;
- if(i + 1 < a) {
- if(y[i+1][j] == '.' && !vis[i+1][j]) {
- dfs(i + 1, j, vis, y);
- }
- }
- if(j + 1 < b) {
- if(y[i][j+1] == '.' && !vis[i][j + 1]) {
- dfs(i, j + 1, vis, y);
- }
- }
- if(i - 1 >= 0) {
- if(y[i-1][j] == '.' && !vis[i-1][j]) {
- dfs(i-1, j, vis, y);
- }
- }
- if(j - 1 >= 0) {
- if(y[i][j-1] == '.' && !vis[i][j - 1]) {
- dfs(i, j - 1, vis, y);
- }
- }
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cin >> a >> b;
- vector<vector<char>> y(a, vector<char> (b));
- for (int i = 0; i<a; i++) {
- for (int j = 0; j<b; j++) {
- cin >> y[i][j];
- }
- }
- vector<vector<int>> vis(1002, vector<int> (1002, 0));
- int ans = 0;
- for (int i = 0; i<a; i++) {
- for (int j = 0; j<b; j++) {
- if(y[i][j] == '.' && !vis[i][j]) {
- ans++;
- dfs(i, j, vis, y);
- }
- }
- }
- cout << ans << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement