C - Humidifier 3 Editorial by en_translator
This is an exercise of BFS (Breadth-First Search) with multiple starting points.
Consider the cells of the grid as the vertices, and add edges between two adjacent non-wall cells. Then this problem asks to find the number of vertices reachable from a vertex with a humidifier with \(D\) moves or less.
If there is only one vertex with a humidifier, it can be solved with an ordinary BFS. First, insert the cell with the humidifier to a queue.
Then, while queue is non-empty, repeat taking out the frontmost element from the queue, and putting unvisited adjacent cells as well as the distances into the queue.
Same applies to the case where there are multiple starting points. We can simply put all the starting points to the queue initially, and the remaining process can be done in exactly the same way as the ordinary BFS.
The complexity is \(\mathrm{O}(HW)\).
posted:
last update: