130. Surrounded Regions
题目描述
Given a 2D board containing ‘X’ and ‘O’ (the letter 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,
|
|
After running your function, the board should be:
|
|
题目大意
将矩阵中,被X包围的所有O都变为X。
解题思路
从四边开始遍历,如果边缘有
O,则继续进行广度遍历,将相连的O标记为#。
遍历矩阵,则剩下的O便为被X包围的,即修改为X;将标记为#的修改为O。
代码
|
|