File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ from collections import deque
2
+
3
+ def bfs (i , j ):
4
+ visited [i ][j ] = True
5
+ s , w = 0 , 0
6
+ que = deque ([(i , j )])
7
+ while que :
8
+ x , y = que .popleft ()
9
+ if array [x ][y ] == 'k' :
10
+ s += 1
11
+ elif array [x ][y ] == 'v' :
12
+ w += 1
13
+ for k in range (4 ):
14
+ nx = x + dx [k ]
15
+ ny = y + dy [k ]
16
+ if 0 <= nx < r and 0 <= ny < c :
17
+ if not visited [nx ][ny ] and array [nx ][ny ] != '#' :
18
+ que .append ((nx , ny ))
19
+ visited [nx ][ny ] = True
20
+ if s > w :
21
+ return [s , 0 ]
22
+ else :
23
+ return [0 , w ]
24
+
25
+ r , c = map (int , input ().split ())
26
+ array = [list (input ()) for _ in range (r )]
27
+ visited = [[False ] * c for _ in range (r )]
28
+ dx = [0 , 0 , 1 , - 1 ]
29
+ dy = [1 , - 1 , 0 , 0 ]
30
+ sheep , wolf = 0 , 0
31
+
32
+ for i in range (r ):
33
+ for j in range (c ):
34
+ if array [i ][j ] in ['v' , 'k' ] and not visited [i ][j ]:
35
+ result = bfs (i , j )
36
+ sheep += result [0 ]
37
+ wolf += result [1 ]
38
+ print (sheep , wolf )
You can’t perform that action at this time.
0 commit comments