Josif_tepe

Untitled

Mar 4th, 2026
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <queue>
  5. using namespace std;
  6. typedef long long ll;
  7. const int INF = 2e9;
  8. const int di[4] = {-1, 1, 0, 0};
  9. const int dj[4] = {0, 0, -1, 1};
  10. int count_rooms(vector<vector<char>> & mat, int n, int m) {
  11.     vector<vector<bool>> visited(n, vector<bool>(m, false));
  12.    
  13.     int res = 0;
  14.     for(int i = 0; i < n; i++) {
  15.         for(int j = 0; j < m; j++) {
  16.             if(mat[i][j] == '.' and !visited[i][j]) {
  17.                 res++;
  18.                 queue<int> q;
  19.                 q.push(i);
  20.                 q.push(j);
  21.                
  22.                 while(!q.empty()) {
  23.                     int ci = q.front();
  24.                     q.pop();
  25.                     int cj = q.front();
  26.                     q.pop();
  27.                    
  28.                     for(int k = 0; k < 4; k++) {
  29.                         int ti = ci + di[k];
  30.                         int tj = cj + dj[k];
  31.                        
  32.                         if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) {
  33.                             q.push(ti);
  34.                             q.push(tj);
  35.                             visited[ti][tj] = true;
  36.                         }
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.     }
  42.    
  43.     return res;
  44.    
  45. }
  46. int main() {
  47.     ios_base::sync_with_stdio(false);
  48.     ifstream cin("in.txt");
  49.     int n, m;
  50.     cin >> n >> m;
  51.     vector<vector<char>> mat(n, vector<char>(m));
  52.    
  53.     for(int i = 0; i < n; i++) {
  54.         for(int j = 0; j < m; j++) {
  55.             cin >> mat[i][j];
  56.         }
  57.     }
  58.    
  59.     int res = INF;
  60.     for(int row = 0; row < n; row++) {
  61.         vector<vector<char>> tmp(n - 1);
  62.         int ci = 0;
  63.         for(int i = 0; i < n; i++) {
  64.             if(row != i) {
  65.                 for(int j = 0; j < m; j++) {
  66.                     tmp[ci].push_back(mat[i][j]);
  67.                 }
  68.                 ci++;
  69.             }
  70.         }
  71.         res = min(res, count_rooms(tmp, n - 1, m));
  72.     }
  73.    
  74.     for(int col = 0; col < m; col++) {
  75.         vector<vector<char>> tmp(n);
  76.         for(int i = 0; i < n; i++) {
  77.             for(int j = 0; j < m; j++) {
  78.                 if(j != col) {
  79.                     tmp[i].push_back(mat[i][j]);
  80.                 }
  81.             }
  82.         }
  83.         res = min(res, count_rooms(tmp, n, m - 1));
  84.     }
  85.     cout << res << endl;
  86.    
  87.     return 0;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment