Advertisement
Josif_tepe

Untitled

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