You are given a grid, where each square is a floor square or a wall square. The symbol .
means a floor square and the symbol #
means a wall square. All boundary squares are wall squares.
Your task is to count the number of rooms in the grid. Two floor squares belong to the same room if they are adjacent horizontally or vertically.
In a file rooms.py
, implement the function count_rooms
, whose parameter is the description of a grid as a list of strings. The function should return the number of rooms in the grid.
def count_rooms(grid): # TODO if __name__ == "__main__": grid = ["########", "#.#..#.#", "#####..#", "#...#..#", "########"] print(count_rooms(grid)) # 4 grid = ["########", "#......#", "#.####.#", "#......#", "########"] print(count_rooms(grid)) # 1 grid = ["########", "######.#", "##.#####", "########", "########"] print(count_rooms(grid)) # 2