Day 17: Surrounded Regions
Problem: You are given a grid of cells containing either "X" or "O". Flip all "surrounded" Os to Xs. An O is surrounded if it is part of a region of adjacent Os that does not touch the border of the grid.
My approach: Flood fill the grid. Cells with an "O" are open to the flood, and cells with an "X" block it. The fill reaches every "border" cell and every cell that's adjacent to a filled cell (no diagonals). Finally, fill in all the cells that aren't touched by the fill.
Definitely save partial work, you'll be revisiting cells fairly often. You can do this in-place if you store results in the grid. Make sure you format your code nicely, because you'll have to repeat things four times (one per direction) with small variations.
My advice for this problem is pretty generic. Flood-fill isn't trivial, but the individual steps are pretty simple. Test as you go, and make sure each step is correct, independently? Hopefully you're doing that already.
54 LOC; expect variation.











