1. 程式人生 > >[LeetCode] 130. Surrounded Regions

[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';
            }
    }
}