Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <string>
- using namespace std;
- int n,m;
- vector<string> arr;
- vector<vector<bool>> vis;
- // indices in bounds
- bool val(int x,int y)
- {
- return 0<=x && x<n && 0<=y && y<m;
- }
- int dfs(char c, int x,int y)
- {
- vis[x][y] = true;
- int sum = 0;
- if(arr[x][y]=='.') sum++;
- vector<pair<int,int>> dirs = {{-1,0},{1,0},{0,-1},{0,1}};
- for(auto dir:dirs)
- {
- int xc = x+dir.first;
- int yc = y+dir.second;
- if(val(xc,yc) && (arr[xc][yc]==c || arr[xc][yc]==' ' || arr[xc][yc]=='.' ) && !vis[xc][yc])
- {
- sum += dfs(c, xc,yc);
- }
- }
- return sum;
- }
- int main()
- {
- cin>>n>>m;
- arr.resize(n);
- vis.assign(n, vector<bool>(m));
- getline(cin, arr[0]);
- for(int i=0;i<n;i++) getline(cin,arr[i]);
- // total number of dots
- int total = 0;
- for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(arr[i][j]=='.') total++;
- // number of dots found
- int cur = 0;
- int need = 0;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<m;j++)
- {
- // it its a letter A-W
- if( 0<=(arr[i][j]-'A') && (arr[i][j]-'A') <= 22 )
- {
- int found = dfs(arr[i][j],i,j);
- // if we find at least 1 new dot
- if(found>0)
- {
- need++;
- total -= found;
- }
- }
- }
- }
- cout << need << ' '<<total<<'\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment