Advertisement
Josif_tepe

Untitled

Mar 2nd, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 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 di[] = {-1, 1, 0, 0};
  25.     int dj[] = {0, 0, -1, 1};
  26.     int preziveani_ovci = 0;
  27.     int preziveani_volci = 0;
  28.     for(int i = 0; i < n; i++) {
  29.         for(int j = 0; j < m; j++) {
  30.             if(mat[i][j] != '#' and !visited[i][j]) {
  31.                 queue<int> q;
  32.                 q.push(i); // indeks na redicata
  33.                 q.push(j); // indeks na kolonata
  34.                 visited[i][j] = true;
  35.                 int volci = 0;
  36.                 int ovci = 0;
  37.                 while(!q.empty()) {
  38.                     int ci = q.front();
  39.                     q.pop();
  40.                     int cj = q.front();
  41.                     q.pop();
  42.                     if(mat[ci][cj] == 'v') {
  43.                         volci += 1;
  44.                     }
  45.                     if(mat[ci][cj] == 'o') {
  46.                         ovci += 1;
  47.                     }
  48.                     for(int k = 0; k < 4; k++) {
  49.                         int ti = ci + di[k];
  50.                         int tj = cj + dj[k];
  51.                         if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#' and !visited[ti][tj]) {
  52.                             visited[ti][tj] = true;
  53.                             q.push(ti);
  54.                             q.push(tj);
  55.                         }
  56.                     }
  57.                 }
  58.                 if(ovci > volci) {
  59.                     preziveani_ovci += ovci;
  60.                 }
  61.                 else {
  62.                     preziveani_volci += volci;
  63.                 }
  64.             }
  65.         }
  66.     }
  67.     cout << preziveani_ovci << " " << preziveani_volci << endl;
  68.     return 0;
  69. }
  70. /*
  71.  ###
  72.  voo
  73.  ###
  74.  
  75.  
  76.  */
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement