import sys
from collections import deque
# sys.stdin = open('input.txt', 'r')
input = sys.stdin.readline
h, w = map(int, input().split())
empty, fox, rabbit = 0, 1, 2
mapping = {'.': empty, 'F': fox, 'R': rabbit}
grid = []
for _ in range(h):
grid.append([mapping[x] for x in input().strip()])
# add padding!
grid.append([0]*len(grid[0]))
for row in grid:
row.append(0)
queue = deque([(0, 0)])
curr_color = grid[0][0]
grid[0][0] = 0
color_cnt = 1
other_color_cnt = 0
ans = 0
while queue:
i, j = queue.popleft()
# if not grid[i][j]: # already visited
# continue
# curr_color = grid[i][j]
# grid[i][j] = 0 # mark visited
for ni, nj in ((i+1, j), (i-1, j), (i, j+1), (i, j-1)):
if grid[ni][nj]:
if grid[ni][nj] == curr_color:
queue.appendleft((ni, nj))
color_cnt += 1
else:
queue.append((ni, nj))
other_color_cnt += 1
grid[ni][nj] = 0
color_cnt -= 1
# print(color_cnt)
# when we run out of the current color in the bfs queue (or when it 0/1 bfs switches from 0 to 1)
if not color_cnt:
color_cnt = other_color_cnt
other_color_cnt = 0
curr_color = not (curr_color-1) + 1 # switch the color
ans += 1
print(ans)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
134 ms |
27960 KB |
Output isn't correct |
2 |
Incorrect |
40 ms |
18648 KB |
Output isn't correct |
3 |
Incorrect |
50 ms |
19664 KB |
Output isn't correct |
4 |
Incorrect |
114 ms |
26344 KB |
Output isn't correct |
5 |
Incorrect |
77 ms |
22468 KB |
Output isn't correct |
6 |
Incorrect |
44 ms |
18544 KB |
Output isn't correct |
7 |
Incorrect |
49 ms |
19768 KB |
Output isn't correct |
8 |
Incorrect |
64 ms |
20280 KB |
Output isn't correct |
9 |
Incorrect |
51 ms |
20260 KB |
Output isn't correct |
10 |
Incorrect |
81 ms |
22312 KB |
Output isn't correct |
11 |
Incorrect |
83 ms |
22724 KB |
Output isn't correct |
12 |
Incorrect |
86 ms |
23956 KB |
Output isn't correct |
13 |
Incorrect |
74 ms |
22472 KB |
Output isn't correct |
14 |
Incorrect |
76 ms |
22236 KB |
Output isn't correct |
15 |
Incorrect |
116 ms |
26700 KB |
Output isn't correct |
16 |
Incorrect |
124 ms |
27912 KB |
Output isn't correct |
17 |
Incorrect |
100 ms |
26328 KB |
Output isn't correct |
18 |
Incorrect |
109 ms |
26364 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
82 ms |
23012 KB |
Output isn't correct |
2 |
Incorrect |
266 ms |
44980 KB |
Output isn't correct |
3 |
Incorrect |
1131 ms |
272624 KB |
Output isn't correct |
4 |
Incorrect |
366 ms |
78164 KB |
Output isn't correct |
5 |
Correct |
914 ms |
162276 KB |
Output is correct |
6 |
Execution timed out |
2072 ms |
319580 KB |
Time limit exceeded |
7 |
Incorrect |
80 ms |
23080 KB |
Output isn't correct |
8 |
Incorrect |
79 ms |
22992 KB |
Output isn't correct |
9 |
Incorrect |
91 ms |
22612 KB |
Output isn't correct |
10 |
Incorrect |
78 ms |
22300 KB |
Output isn't correct |
11 |
Incorrect |
78 ms |
22372 KB |
Output isn't correct |
12 |
Correct |
71 ms |
20516 KB |
Output is correct |
13 |
Incorrect |
231 ms |
44932 KB |
Output isn't correct |
14 |
Incorrect |
172 ms |
35428 KB |
Output isn't correct |
15 |
Incorrect |
167 ms |
35416 KB |
Output isn't correct |
16 |
Incorrect |
153 ms |
31780 KB |
Output isn't correct |
17 |
Incorrect |
467 ms |
84232 KB |
Output isn't correct |
18 |
Incorrect |
415 ms |
79308 KB |
Output isn't correct |
19 |
Incorrect |
361 ms |
78064 KB |
Output isn't correct |
20 |
Incorrect |
334 ms |
72704 KB |
Output isn't correct |
21 |
Incorrect |
724 ms |
162924 KB |
Output isn't correct |
22 |
Correct |
928 ms |
162432 KB |
Output is correct |
23 |
Incorrect |
824 ms |
148444 KB |
Output isn't correct |
24 |
Incorrect |
744 ms |
162344 KB |
Output isn't correct |
25 |
Incorrect |
1353 ms |
259476 KB |
Output isn't correct |
26 |
Correct |
1852 ms |
341528 KB |
Output is correct |
27 |
Execution timed out |
2083 ms |
327956 KB |
Time limit exceeded |
28 |
Execution timed out |
2098 ms |
319640 KB |
Time limit exceeded |
29 |
Execution timed out |
2087 ms |
328464 KB |
Time limit exceeded |
30 |
Execution timed out |
2096 ms |
327472 KB |
Time limit exceeded |
31 |
Incorrect |
1798 ms |
206820 KB |
Output isn't correct |
32 |
Execution timed out |
2095 ms |
323672 KB |
Time limit exceeded |