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.

Tips and Divergent thinking:

N/A

(全文完,原创文章,转载时请注明作者和出处)


(转载本站文章请注明作者和出处 烟客旅人 sigmainfy — http://www.sigmainfy.com,请勿用于任何商业用途)

Written on June 15, 2013