Advertisement
tepyotin2

Cow Art Problem

Oct 11th, 2023
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. vector<string> colors;
  7.  
  8. int dx[] = {1, -1, 0, 0};
  9. int dy[] = {0, 0, -1, 1};
  10.  
  11. bool visited[101][101];
  12. // int groups[101][101];
  13.  
  14. bool isCow = false;
  15.  
  16. char getColor(char col){
  17.     if(isCow && col == 'G'){
  18.         col = 'R';
  19.     }
  20.     return col;
  21. }
  22.  
  23. void floodfill(int x, int y, int ngroup){
  24.     if(x<0 || x>n-1 || y<0 || y>n-1){
  25.         return;
  26.     }
  27.     if(visited[x][y]){
  28.         return;
  29.     }
  30.     visited[x][y] = true;
  31.     // groups[x][y] = ngroup;
  32.     for(int i=0; i<4; i++){
  33.         int nx = x+dx[i], ny = y+dy[i];
  34.         if(getColor(colors[nx][ny]) == getColor(colors[x][y])){
  35.             floodfill(nx, ny, ngroup);
  36.         }
  37.     }
  38. }
  39.  
  40. int main(){
  41.     ios_base::sync_with_stdio(0), cin.tie(0);
  42.     freopen("cowart.in", "r", stdin);
  43.     freopen("cowart.out", "w", stdout);
  44.     cin >> n;
  45.     string col;
  46.     for(int i=0; i<n; i++){
  47.         cin >> col;
  48.         // cout << "col: " << col << '\n';
  49.         // for(int j=0; j<n; j++){
  50.         //     cout << col[i] << "\n";
  51.         //     colors[i][j] = col[i];
  52.         // }
  53.         colors.push_back(col);
  54.     }
  55.     // for(int i=0; i<n; i++){
  56.     //     for(int j=0; j<n; j++){
  57.     //         cout << "i: " << i << ", j: " << j << ", colors[i][j]: " << colors[i][j] << ", ngroup: " << groups[i][j] << ", ";
  58.     //         // if(!visited[i][j]){
  59.     //         //     floodfill(i, j, ngroup);
  60.     //         //     ngroup++;
  61.     //         // }
  62.     //     }
  63.     //     // cout << "i: " << colors << '\n';
  64.     //     cout << '\n';
  65.     // }
  66.     int ngroup = 0;
  67.     for(int i=0; i<n; i++){
  68.         for(int j=0; j<n; j++){
  69.             // cout << "i: " << i << ", j: " << j << ", colors[i][j]: " << colors[i][j] << ", ";
  70.             if(!visited[i][j]){
  71.                 floodfill(i, j, ngroup);
  72.                 ngroup++;
  73.             }
  74.         }
  75.         // cout << "i: " << colors << '\n';
  76.         // cout << '\n';
  77.     }
  78.     int human = ngroup;
  79.     // int human = groups[n-1][n-1];
  80.     isCow = true;
  81.     ngroup = 0;
  82.     memset(visited, false, sizeof(visited));
  83.     for(int i=0; i<n; i++){
  84.         for(int j=0; j<n; j++){
  85.             // cout << "i: " << i << ", j: " << j << ", colors[i][j]: " << colors[i][j] << ", ";
  86.             if(!visited[i][j]){
  87.                 floodfill(i, j, ngroup);
  88.                 ngroup++;
  89.             }
  90.         }
  91.         // cout << "i: " << colors << '\n';
  92.         // cout << '\n';
  93.     }
  94.     int cow = ngroup;
  95.     cout << human << " " << cow << '\n';
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement