Python · July 5, 2022 0

CCC ’03 S3 – Floor Plan

Floor plan is a CCC senior 3 problem that is very confusing at first, and gradually becomes more understandable. IndexError is more common.

Functions greatly aid to help make the code for the solution.

First, make a function called findarea. This function, given a coordinate, will find the area of the room using if statements.

Another function called floorareas will be helpful for the future. It will search through the entire floor plan, and using the findarea function, will return a list of the sizes of all the rooms. This will be extremely useful.

The last parts are the simplest. Taking a list of the sizes from the floorareas function, it will calculate how many rooms the flooring could take and the amount of flooring left over. Then, output it.

Sample solution:

def findarea(n, m, house, row, column):
    total = 1
    points = [[n, m]]
    house[n][m] = 2
    for _ in range(65):
        for u in range(len(points)):
            i = points[u][0]
            j = points[u][1]
            if i > 0:
                if house[i-1][j] == 1:
                    total += 1
                    house[i-1][j] = 2
                    points.append([i-1, j])
            if i < row - 1:
                if house[i+1][j] == 1:
                    total += 1
                    house[i+1][j] = 2
                    points.append([i+1, j])
            if j < column - 1:
                if house[i][j+1] == 1:
                    total += 1
                    house[i][j+1] = 2
                    points.append([i, j+1])
            if j > 0:
                if house[i][j-1] == 1:
                    total += 1
                    house[i][j-1] = 2
                    points.append([i, j-1])
    return total, house


def floorareas(house, col):
    areas = []
    for i in range(len(house)):
        for j in range(col):
            if house[i][j] == 1:
                c = findarea(i, j, house, rows, columns)
                house = c[1]
                areas.append(c[0])
    return areas


flooring, rows, columns = int(input()), int(input()), int(input())
plan = []
for k in range(rows):
    h = list(input())
    for i in range(len(h)):
        if h[i] == '.':
            h[i] = 1
        elif h[i] == 'I':
            h[i] = 0
    plan.append(h)
spaces = floorareas(plan, columns)
spaces = sorted(spaces)[::-1]
floors = 0
for i in spaces:
    if i > flooring:
        break
    else:
        floors += 1
        flooring -= i
if floors == 1:
    print('1 room,', flooring, 'square metre(s) left over')
else:
    print(floors, 'rooms,', flooring, 'square metre(s) left over')