Floor Plan
InterviewTime limit1sMemory limit128 MB
Given a grid of walls and floor cells, count connected rooms, sort them by size, floor as many of the largest rooms as the wood supply allows, and report how many rooms got flooring plus the leftover wood.
- Level
Medium4 of 10
- Topics
- DFS, Sorting, Implementation, Array
- Solved
- No attempts yet
Problem
The floor plan of a house is given on a grid. The character I is a wall and the character . is room floor space; each character occupies square metre. Doorways are not shown.
A room is a maximal group of . cells connected horizontally or vertically. No room is larger than square metres.
You are given a supply of hardwood flooring measured in square metres. You install the flooring starting with the largest room, then the next largest, and so on. Flooring a room consumes an amount of wood equal to its area, you may not skip any room, and you must stop as soon as you do not have enough wood for the next room.
Output how many rooms receive flooring and how many square metres of flooring are left over.
Input
- Line 1: an integer, the number of square metres of flooring you have.
- Line 2: an integer (), the number of rows in the grid.
- Line 3: an integer (), the number of columns in the grid.
- The next lines each contain characters of grid data (
Ior.).
Output
Print exactly one line in the format
K rooms, M square metre(s) left over
where is the number of rooms that received flooring and is the number of square metres left over.