Advertisement
Josif_tepe

Untitled

Oct 28th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main() {
  7.     int n, m;
  8.     cin >> n >> m;
  9.    
  10.     char mat[n][m];
  11.     for(int i = 0; i < n; i++) {
  12.         for(int j = 0; j < m; j++) {
  13.             cin >> mat[i][j];
  14.         }
  15.     }
  16.     int res_ovci = 0, res_volci = 0;
  17.     vector<vector<bool>> visited(n, vector<bool>(m, false));
  18.     for(int i = 0 ; i < n; i++) {
  19.         for(int j = 0; j < m; j++) {
  20.             if(mat[i][j] != '#' and !visited[i][j]) {
  21.                 queue<int> q;
  22.                 q.push(i);
  23.                 q.push(j);
  24.                 int ovci = 0, volci = 0;
  25.                 visited[i][j] = true;
  26.                
  27.                 while(!q.empty()) {
  28.                     int ci = q.front();
  29.                     q.pop();
  30.                     int cj = q.front();
  31.                     q.pop();
  32.                    
  33.                     if(mat[ci][cj] == 'o') {
  34.                         ovci++;
  35.                     }
  36.                     if(mat[ci][cj] == 'v') {
  37.                         volci++;
  38.                     }
  39.                    
  40.                     if(ci + 1 < n and mat[ci + 1][cj] != '#' and !visited[ci + 1][cj]) {
  41.                         q.push(ci + 1);
  42.                         q.push(cj);
  43.                         visited[ci + 1][cj] = true;
  44.                     }
  45.                     if(ci - 1 >= 0 and mat[ci - 1][cj] != '#' and !visited[ci - 1][cj]) {
  46.                         q.push(ci - 1);
  47.                         q.push(cj);
  48.                         visited[ci - 1][cj] = true;
  49.                     }
  50.                     if(cj + 1 < m and mat[ci][cj + 1] != '#' and !visited[ci][cj + 1]) {
  51.                         q.push(ci);
  52.                         q.push(cj + 1);
  53.                         visited[ci][cj + 1] = true;
  54.                     }
  55.                     if(cj - 1 >= 0 and mat[ci][cj - 1] != '#' and !visited[ci][cj - 1]) {
  56.                         q.push(ci);
  57.                         q.push(cj - 1);
  58.                         visited[ci][cj - 1] = true;
  59.                     }
  60.                 }
  61.                 if(ovci > volci) {
  62.                     res_ovci += ovci;
  63.                 }
  64.                 else {
  65.                     res_volci += volci;
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     cout << res_ovci << " " << res_volci << endl;
  71.     return 0;
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement