CSES - Walls and floors

A grid has n \times n squares, which are all walls initially. The rows and columns are numbered 1 \dots n. The walls are then gradually removed by converting them to floors.

Your task is to keep track of the number of rooms during the process. A room consists of a vertically or horizontally connected floor squares, as in earlier problems.

You may assume that n is at most 100 and that the methods of the class are called at most 10000 times. The wall squares at the borders of the grid are never removed.

In a file wallgrid.py, implement a class WallGrid with the following methods:

  • constructor with the size of the grid as a parameter
  • remove converts he square (x,y) into a floor if it is not already a floor
  • count returns the number of rooms
class WallGrid:
    def __init__(self,n):
        # TODO

    def remove(self,x,y):
        # TODO

    def count(self):
        # TODO

if __name__ == "__main__":
    w = WallGrid(5)
    print(w.count()) # 0
    w.remove(2,2)
    w.remove(4,2)
    print(w.count()) # 2
    w.remove(3,2)
    print(w.count()) # 1
    w.remove(2,4)
    w.remove(2,4)
    w.remove(4,4)
    print(w.count()) # 3
    w.remove(3,3)
    print(w.count()) # 3
    w.remove(3,4)
    print(w.count()) # 1