Rings

From programming_contest
Jump to navigation Jump to search

This problem asks us to label boxes on a grid based on the distance they are from the "outside". The algorithm is quite easy. BFS from all the outside nodes inwawrd and remember their distances....but printing the output is a pain.

To start, we have to find all the "bark" nodes which will serve as start points for the BFS. To do this, we walk around the edge of the grid and add any tree spots we find to our queue with distance 1, and when we reach any non-tree nodes, we flood fill until we find tree nodes, and then add them to our queue as well.

Now we simply process our BFS queue, maintaining the distances to each node.

To print, we need to walk the input grid and print either:

  1. two dots if the input is a dot
  2. dot plus digit if the number is less than 10
  3. number if it is more than 10