1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Main {
5+ static int R ,C ;
6+ static char arr [][];
7+ static int dx [] = {-1 ,1 ,0 ,0 };
8+ static int dy [] = {0 ,0 ,-1 ,1 };
9+ static int sheep =0 ;
10+ static int wolf = 0 ;
11+ static boolean visited [][];
12+ public static void main (String [] args ) throws IOException {
13+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
14+
15+ StringTokenizer st =new StringTokenizer (br .readLine ());
16+
17+ R = Integer .parseInt (st .nextToken ());
18+ C = Integer .parseInt (st .nextToken ());
19+
20+ arr = new char [R ][C ];
21+ visited = new boolean [R ][C ];
22+
23+ for (int i =0 ; i <R ; i ++) {
24+ String input = br .readLine ();
25+ for (int j =0 ; j <C ; j ++) {
26+ arr [i ][j ]= input .charAt (j );
27+ }
28+ }
29+ for (int i =0 ; i <R ; i ++) {
30+ for (int j =0 ; j <C ; j ++) {
31+ char currentChar = arr [i ][j ];
32+ if (currentChar == '.' || currentChar == 'v' || currentChar =='o' ) {
33+ if (visited [i ][j ] == false ) {
34+ bfs (i ,j );
35+ }
36+ }
37+ }
38+ }
39+ System .out .print (sheep +" " +wolf );
40+
41+ }
42+
43+ static void bfs (int x , int y ) {
44+ int currentSheep = 0 ;
45+ int currentWolf = 0 ;
46+ Queue <int []> queue = new LinkedList <>();
47+ queue .add (new int []{x ,y });
48+ visited [x ][y ]= true ;
49+
50+ if (arr [x ][y ] == 'o' )
51+ currentSheep ++;
52+
53+ if (arr [x ][y ] =='v' ) {
54+ currentWolf ++;
55+ }
56+
57+ while (!queue .isEmpty ()) {
58+
59+ int now [] = queue .poll ();
60+ int nowx = now [0 ];
61+ int nowy = now [1 ];
62+
63+ for (int i =0 ; i <4 ; i ++) {
64+ int nx = nowx + dx [i ];
65+ int ny = nowy + dy [i ];
66+
67+ if (nx >=0 && nx <R && ny >=0 && ny <C ) {
68+ if (visited [nx ][ny ] == false && arr [nx ][ny ] != '#' ) {
69+ queue .add (new int [] {nx ,ny });
70+ visited [nx ][ny ] = true ;
71+
72+ if (arr [nx ][ny ] == 'o' )
73+ currentSheep ++;
74+ if (arr [nx ][ny ] == 'v' ) {
75+ currentWolf ++;
76+ }
77+ }
78+ }
79+ }
80+ }
81+
82+ if (currentSheep > currentWolf ) {
83+ sheep += currentSheep ;
84+ }
85+ else {
86+ wolf += currentWolf ;
87+ }
88+ }
89+ }
0 commit comments