CCCPaste

Untitled

#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 500;
const int di[] = {-1, 1, 0, 0};
const int dj[] = {0, 0, -1, 1};
int n, m;
char mat[maxn][maxn];
bool visited[maxn][maxn];
int wolves, sheep;
void dfs(int i, int j) {
//    cout << i << " " << j << endl;
    visited[i][j] = true;
    
    if(mat[i][j] == 'v') {
        wolves++;
    }
    if(mat[i][j] == 'o') {
        sheep++;
    }
    for(int k = 0; k < 4; k++) {
        int ti = i + di[k];
        int tj = j + dj[k];
        
        if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and mat[ti][tj] != '#') {
            dfs(ti, tj);
        }
    }
    
}
int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> m;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> mat[i][j];
            visited[i][j] = false;
        }
    }
    int res_sheep = 0, res_wolves = 0;
    
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(mat[i][j] != '#' and !visited[i][j]) {
                wolves = 0;
                sheep = 0;
                dfs(i, j);
                if(sheep > wolves) {
                    res_sheep += sheep;
                }
                else {
                    res_wolves += wolves;
                }
            }
        }
    }
    cout << res_sheep << " " << res_wolves << endl;
    return 0;
}
/*
 6 6
 1 2
 2 3
 2 5
 3 5
 3 4
 5 6
 **/