LeetCode 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.

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

Explanation:

Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.

这个题的出发点就是只需要从四个边上的元素出发,进行dfs(这句话添加于2019/09/28二刷)

class Solution {
    
    //将边界上的'O'以及与边界的'O'邻接的'O'表计为'-'
    //然后现在将剩下的'O'都标记为'X'
    //最后将'-'还原为'O'
    
    public void solve(char[][] board) {
        if (board.length==0) return;
        for (int i=0;i

 

你可能感兴趣的:(LeetCode 130. Surrounded Regions)