Advertisement
Josif_tepe

Untitled

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