Skip to content

Instantly share code, notes, and snippets.

@Ladicle
Last active October 26, 2021 13:02
Show Gist options
  • Save Ladicle/b39de7fc1fab59779fb32602c9665aaf to your computer and use it in GitHub Desktop.
Save Ladicle/b39de7fc1fab59779fb32602c9665aaf to your computer and use it in GitHub Desktop.
red and black
BLACK = "."
RED = "#"
HUMAN = "@"
DIRECTIONS = [(-1, 0), (0, 1), (1, 0), (0, -1)] # up right down left
def red_and_black(col, row, point, graph):
stack = [point]
cnt = 0
while stack:
y, x = stack.pop()
if graph[y][x] == RED:
continue
cnt += 1
graph[y][x] = RED
for dy, dx in DIRECTIONS:
if 0 <= y + dy < row and 0 <= x + dx < col and graph[y + dy][x + dx] == BLACK:
stack.append((y + dy, x + dx))
return cnt
while True:
col, row = map(int, input().split())
if col == 0 and row == 0: break
graph = []
x, y = -1, -1
for i in range(row):
line = input()
graph.append(list(line))
if (idx := line.find(HUMAN)) != -1:
y, x = i, idx
print(red_and_black(col, row, (y, x), graph))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment