[LeetCode] 130. Surrounded Regions

题:https://leetcode.com/problems/surrounded-regions/description/

题目

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.

思路

题目大意

由 “*”和“O”组成的矩阵。将被“X”包括的“O”的变为 “X”。

解题思路

两种“O”需要被“X”换掉:

  1. “O”位于矩阵的边缘。
  2. “O”直接或间接连接的“O”位于矩阵的边缘。

所以,解题步骤,
1.找到边缘的“O”。然后对其进行BFS或DFS,遍历所连接的“O”,设为“1”。
2.遍历每个矩阵,对于“O”设置为“X”,对于“1”设置为“O”。

代码NOTE:

1.DFS、BFS直接用 一个 queue加while len(queue)!=0。来实现,不用递归。
2.判断的方式:可以先放入queue中,然后 判断 是否 越界或满足相应的要求。

def makeUnsurroundedRejion(board, i, j, m, n):
    queue = [(i, j)]
    direction = [(0, 1), (1, 0), (-1, 0), (0, -1)]

    while len(queue) != 0:
        i, j = queue[-1]
        del queue[-1]
        if 0 <= i < m and 0 <= j < n and board[i][j] == 'O':
            board[i][j] = '1'
            for eleDirection in direction:
                queue.append((i + eleDirection[0], j + eleDirection[1]))


class Solution:
    def solve(self, board):
        """
        :type board: List[List[str]]
        :rtype: void Do not return anything, modify board in-place instead.
        """
        m = len(board)
        if m == 0:
            return;

        n = len(board[0])
        start_pos = [(0, 0), (m - 1, 0), (1, 0), (1, n - 1)]
        step = [(0, 1), (0, 1), (1, 0), (1, 0)]
        for k in range(4):
            i, j = start_pos[k]
            while 0 <= i < m and 0 <= j < n:
                if board[i][j] == 'O':
                    makeUnsurroundedRejion(board, i, j, m, n)
                i += step[k][0]
                j += step[k][1]

        for i in range(m):
            for j in range(n):
                board[i][j] = "O" if board[i][j] == "1" else "X"

第二版

思想和 第一版相同,
采用 的 DFS ,用边界去扩展。

class Solution {
    
    static final int[][] directions = {{-1,0},{1,0},{0,-1},{0,1}};
    
    public void mark(char[][] board,int r,int c,int rows,int cols){
        if(!(r>=0 && r<rows && c>=0 && c<cols) || board[r][c]!='O')
            return;
        board[r][c]='M';
        for(int[] direction : directions)
            mark(board,r+direction[0],c+direction[1],rows,cols);
    }
    
    public void solve(char[][] board) {
        int rows = board.length;
        if(rows == 0 )
            return ;
        int cols = board[0].length;
        for(int i = 0 ; i <rows;i++){
            if(board[i][0]=='O')
                mark(board,i,0,rows,cols);
            if(board[i][cols-1]=='O')
                mark(board,i,cols-1,rows,cols);
        }
    
        for(int j = 0 ; j< cols;j++){
            if(board[0][j]=='O')
                mark(board,0,j,rows,cols);
            if(board[rows-1][j]=='O')
                mark(board,rows-1,j,rows,cols);        
        }
        
        for(int i = 0 ; i <rows;i++)
            for(int j = 0 ; j< cols;j++){
                if(board[i][j]=='M')
                    board[i][j] ='O';
                else
                    board[i][j] = 'X';
            }
    }
}

猜你喜欢

转载自blog.csdn.net/u013383813/article/details/84350555