leetcode: Surrounded Regions

Problem Description:

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region .

For example,

X X X X
X O O X
X X O X
X O X X

After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X

Solution and Precautions:

Scan the four edges of the given matrix, if encounter any ’0′, then start from this ’0′ use either BFS or DFS to mark all the connected ’0′ area by some marks say ‘#’, after doing this, all the cells with ‘#’ mark should remain to be ’0′ while other cells should be set ‘X’ no matter what it previously was.

Written on June 15, 2013