130. 被圍繞的區域

Better-1發表於2020-11-13

題目不難,慣用的廣度優先,答題卻慘不忍睹:多種情況的考慮不周全,粗心導致一些變數混亂。。。。。。

給定一個二維的矩陣,包含 ‘X’ 和 ‘O’(字母 O)。
找到所有被 ‘X’ 圍繞的區域,並將這些區域裡所有的 ‘O’ 用 ‘X’ 填充。

在這裡插入圖片描述

官方版本比最下面自己的版本優化一些

# 官方的版本好在他直接把邊緣位置是O的給找到,並且廣度優先找到了所有的O,這些就是不能轉換為X的,其他就都可以。並且官方並不是用字典記錄來增加記憶體,而是記為了另外一個字母,最後一起遍歷的時候再修改回去。
class Solution:
    def solve(self, board: List[List[str]]) -> None:
        if not board:
            return
        
        n, m = len(board), len(board[0])
        que = collections.deque()
        for i in range(n):
            if board[i][0] == "O":
                que.append((i, 0))
            if board[i][m - 1] == "O":
                que.append((i, m - 1))
        for i in range(m - 1):
            if board[0][i] == "O":
                que.append((0, i))
            if board[n - 1][i] == "O":
                que.append((n - 1, i))
        
        while que:
            x, y = que.popleft()
            board[x][y] = "A"
            for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
                if 0 <= mx < n and 0 <= my < m and board[mx][my] == "O":
                    que.append((mx, my))
        
        for i in range(n):
            for j in range(m):
                if board[i][j] == "A":
                    board[i][j] = "O"
                elif board[i][j] == "O":
                    board[i][j] = "X"

自己的版本還是過於冗餘,還要排序。

class Solution:
    def solve(self, board: List[List[str]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        # 本題慘不忍睹,問題出在length和width沒有區分開!!!
        舉例:  [[1,2,3],[2,3,4],[1,2,3],[2,3,4]]length就是4,width就是3.length就是第一維,width就是第二維(第一維的裡面)
        """
        if board != []:

            record = {}
            queue = set()

            length = len(board)
            width = len(board[0])
            direction = [(1, 0), (-1, 0), (0, 1), (0, -1)]

            for i in range(length):
                for j in range(width):
                    if (i, j) in record:
                        continue
                    if board[i][j] != "O":
                        record[(i ,j)] =1
                        continue

                    record1 = []
                    queue.add((i, j))

                    record1.append([i, j])
                    while queue:
                        ex_queue = queue
                        queue = set()
                        for word in ex_queue:
                            m, n = word
                            for dire in direction:
                                dm, dn = dire
                                if m+dm < 0 or m + dm >= length or n + dn < 0 or n + dn >= width:
                                    continue
                                
                                if (m+dm,n+dn) in record:
                                    continue
                                if board[m+dm][n+dn] != "O":
                                    record[(m+dm,n+dn)] = 1
                                    continue
                                record[(m+dm,n+dn)] = 1
                                record1.append([m+dm,n+dn])
                                queue.add((m+dm,n+dn))
                    if not record1:
                        continue
                    record2 = sorted(record1, key=lambda x:x[0])
                    record3 = sorted(record1, key=lambda x:x[1])
                    if record2[0][0] == 0 or record2[-1][0] == length-1:
                        continue
                    if record3[0][1] == 0 or record3[-1][1] == width-1:
                        continue

                    for re in record2:
                        m, n = re
                        board[m][n] = "X"


相關文章