Graph Theory37 sections · 1633 units
Open in Course

Problem - Monsters

(CSES multi-source BFS)

N x M grid. 'A' is you, 'M' is monster, '.' is floor, '#' is wall. You and monsters move simultaneously each second (44 directions). Monsters move to catch you.

Find a path to any boundary cell where you escape before monsters catch you.

This is BFS, but with a twist. What is different from Labyrinth? You have multiple entities moving at once. The monsters also move on each time step. You need to know whether a monster can reach a cell before you do. Multi-source BFS from all monsters at once computes the earliest monster arrival time at every cell.