import sys
H, W = map(int, sys.stdin.readline().split())
mapp = [list(sys.stdin.readline()) for _ in range(H)]
if mapp[0][0] == 'R':
ani = ['R', 'F']
else:
ani = ['F', 'R']
dirs = [(0,1), (1,0), (0,-1), (-1,0)]
count = 0
while True:
for a in ani: # Loops between the two types of animals
flag = 0 # Checks if animal is detected
vis = [[-1]*W for _ in range(H)]
vis[0][0] = 1
queue = [(0,0)]
# BFS
for r, c in queue:
for x, y in dirs:
rr = r+x
cc = c+y
if rr < 0 or rr >= H or cc < 0 or cc >= W:
continue
if vis[rr][cc] == 1:
continue
if mapp[rr][cc] == a:
flag = 1
if mapp[rr][cc] == a or mapp[rr][cc] == '#':
vis[rr][cc] = 1
mapp[rr][cc] = '#'
queue.append((rr, cc))
if flag == 0:
break
count += 1
if flag == 0:
break
print(count)
Compilation message (stdout)
Compiling 'tracks.py'...
=======
adding: __main__.pyc (deflated 38%)
=======
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |