Last active
October 26, 2021 13:02
-
-
Save Ladicle/b39de7fc1fab59779fb32602c9665aaf to your computer and use it in GitHub Desktop.
red and black
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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