Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- vector<string> colors;
- int dx[] = {1, -1, 0, 0};
- int dy[] = {0, 0, -1, 1};
- bool visited[101][101];
- // int groups[101][101];
- bool isCow = false;
- char getColor(char col){
- if(isCow && col == 'G'){
- col = 'R';
- }
- return col;
- }
- void floodfill(int x, int y, int ngroup){
- if(x<0 || x>n-1 || y<0 || y>n-1){
- return;
- }
- if(visited[x][y]){
- return;
- }
- visited[x][y] = true;
- // groups[x][y] = ngroup;
- for(int i=0; i<4; i++){
- int nx = x+dx[i], ny = y+dy[i];
- if(getColor(colors[nx][ny]) == getColor(colors[x][y])){
- floodfill(nx, ny, ngroup);
- }
- }
- }
- int main(){
- ios_base::sync_with_stdio(0), cin.tie(0);
- freopen("cowart.in", "r", stdin);
- freopen("cowart.out", "w", stdout);
- cin >> n;
- string col;
- for(int i=0; i<n; i++){
- cin >> col;
- // cout << "col: " << col << '\n';
- // for(int j=0; j<n; j++){
- // cout << col[i] << "\n";
- // colors[i][j] = col[i];
- // }
- colors.push_back(col);
- }
- // for(int i=0; i<n; i++){
- // for(int j=0; j<n; j++){
- // cout << "i: " << i << ", j: " << j << ", colors[i][j]: " << colors[i][j] << ", ngroup: " << groups[i][j] << ", ";
- // // if(!visited[i][j]){
- // // floodfill(i, j, ngroup);
- // // ngroup++;
- // // }
- // }
- // // cout << "i: " << colors << '\n';
- // cout << '\n';
- // }
- int ngroup = 0;
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++){
- // cout << "i: " << i << ", j: " << j << ", colors[i][j]: " << colors[i][j] << ", ";
- if(!visited[i][j]){
- floodfill(i, j, ngroup);
- ngroup++;
- }
- }
- // cout << "i: " << colors << '\n';
- // cout << '\n';
- }
- int human = ngroup;
- // int human = groups[n-1][n-1];
- isCow = true;
- ngroup = 0;
- memset(visited, false, sizeof(visited));
- for(int i=0; i<n; i++){
- for(int j=0; j<n; j++){
- // cout << "i: " << i << ", j: " << j << ", colors[i][j]: " << colors[i][j] << ", ";
- if(!visited[i][j]){
- floodfill(i, j, ngroup);
- ngroup++;
- }
- }
- // cout << "i: " << colors << '\n';
- // cout << '\n';
- }
- int cow = ngroup;
- cout << human << " " << cow << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement