matbensch

NAQ22 E Solution (C++)

Feb 4th, 2023 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int n,m;
  9. vector<string> arr;
  10. vector<vector<bool>> vis;
  11.  
  12. // indices in bounds
  13. bool val(int x,int y)
  14. {
  15.     return 0<=x && x<n && 0<=y && y<m;
  16. }
  17.  
  18. int dfs(char c, int x,int y)
  19. {
  20.     vis[x][y] = true;
  21.  
  22.     int sum = 0;
  23.     if(arr[x][y]=='.') sum++;
  24.  
  25.     vector<pair<int,int>> dirs = {{-1,0},{1,0},{0,-1},{0,1}};
  26.  
  27.     for(auto dir:dirs)
  28.     {
  29.         int xc = x+dir.first;
  30.         int yc = y+dir.second;
  31.  
  32.         if(val(xc,yc) && (arr[xc][yc]==c || arr[xc][yc]==' ' || arr[xc][yc]=='.' ) && !vis[xc][yc])
  33.         {
  34.             sum += dfs(c, xc,yc);
  35.         }
  36.  
  37.     }
  38.     return sum;
  39. }
  40.  
  41. int main()
  42. {
  43.     cin>>n>>m;
  44.     arr.resize(n);
  45.     vis.assign(n, vector<bool>(m));
  46.    
  47.     getline(cin, arr[0]);
  48.  
  49.     for(int i=0;i<n;i++) getline(cin,arr[i]);
  50.  
  51.     // total number of dots
  52.     int total = 0;
  53.     for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(arr[i][j]=='.') total++;
  54.  
  55.     // number of dots found
  56.     int cur = 0;
  57.     int need = 0;
  58.  
  59.     for(int i=0;i<n;i++)
  60.     {
  61.         for(int j=0;j<m;j++)
  62.         {
  63.             // it its a letter A-W
  64.             if( 0<=(arr[i][j]-'A') && (arr[i][j]-'A') <= 22 )
  65.             {
  66.                 int found = dfs(arr[i][j],i,j);
  67.                 // if we find at least 1 new dot
  68.                 if(found>0)
  69.                 {
  70.                     need++;
  71.                     total -= found;
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     cout << need << ' '<<total<<'\n';
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment