Editorial for OTHS Coding Competition 2 P5 - Coffee Jelly


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Authors: Ivan_Li, volcano

To solve this problem, we can employ a flood fill algorithm. For each unvisited open space, we initiate a flood fill via a depth first search in which we explore all 4 possible directions (up, down, left, right) to find all connected open spaces. During the flood fill, we will keep track of whether we encounter a person. If we do, we mark the current classroom as "not empty". After completing the flood fill for each unvisited open space, if the classroom was found to be empty (i.e., it did not touch any *), we increment our count of empty classrooms. Ensure to reset the variable used to track the state of the room after every DFS.

Time Complexity: \mathcal{O}(nm)

Alternate solution

Start flood fill at every unvisited cell with a * and turn the entire open space containing it into walls. Then count the number of unvisited open spaces using DFS.

Time Complexity: \mathcal{O}(nm)


Comments

There are no comments at the moment.