1、题目描述:
2、题解:
方法1:DFS递归
思路:被包围的区间不会存在于边界上,所以对边界的’O’进行处理就行,只要找到与边界的’O’连通的’O’就可以,变成’#’,然后把其他位置的
‘O’变成’X’。
python代码如下:
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
#DFS递归 寻找与边界连通的'O',改变为'#',然后再次遍历board,使'#'变为'O'
if not board or len(board) == 0:return
m,n = len(board),len(board[0])
#遍历,进行DFS,让与边界连通的‘O’设置为‘#’
for i in range(m):
for j in range(n):
isEdge = (i == 0 or j == 0 or i == m-1 or j == n-1)
if isEdge and board[i][j] == 'O':
self.dfs(board,i,j)
#再次遍历,让'#'变为'O'表示不被'X'围绕;让“O”变 "X".
for i in range(m):
for j in range(n):
if board[i][j] == '#':
board[i][j] = 'O'
elif board[i][j] == 'O':
board[i][j] = 'X'
def dfs(self,board,i ,j):
#边界处理
if i < 0 or j < 0 or i >= len(board) or j >= len(board[0]) or board[i][j] == 'X' or board[i][j] == '#':
return
board[i][j] = '#'
self.dfs(board,i-1,j) #上
self.dfs(board,i+1,j) #下
self.dfs(board,i,j-1) #左
self.dfs(board,i,j+1) #右
方法2:DFS非递归:
思路:被包围的区间不会存在于边界上,所以对边界的’O’进行处理就行,只要找到与边界的’O’连通的’O’就可以,变成’#’,然后把其他位置的
‘O’变成’X’。
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
#DFS非递归
if not board or len(board) == 0:return
m,n = len(board),len(board[0])
for i in range(m):
for j in range(n):
isEdge = (i == 0 or j == 0 or i == m - 1 or j == n - 1)
if isEdge and board[i][j] == 'O':
self.dfs(board,i,j)
for i in range(m):
for j in range(n):
if board[i][j] == 'O':
board[i][j] = 'X'
elif board[i][j] == '#':
board[i][j] = 'O'
def dfs(self,board,i,j):
stack = []
stack.append((i,j))
board[i][j] = '#'
while stack:
#取出栈顶,不弹出
x,y = stack[-1]
#上
if x - 1 >= 0 and board[x - 1][y] == 'O':
stack.append((x-1,y))
board[x-1][y] = '#'
continue
#下
if x + 1 < len(board) and board[x + 1][y] == 'O':
stack.append((x + 1,y))
board[x + 1][y] = '#'
continue
#左
if y - 1 >= 0 and board[x][y - 1] =='O':
stack.append((x,y - 1))
board[x][y- 1] = '#'
continue
#右
if y + 1 < len(board[0]) and board[x][y + 1] == 'O':
stack.append((x,y + 1))
board[x][y + 1] = '#'
continue
#如果上下左右都搜索不到,本次搜索结束,弹出stack
stack.pop()
方法3:BFS非递归
思路:被包围的区间不会存在于边界上,所以对边界的’O’进行处理就行,只要找到与边界的’O’连通的’O’就可以,变成’#’,然后把其他位置的
‘O’变成’X’。
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
#BFS 非递归
if not board or len(board) == 0:return
m,n = len(board),len(board[0])
for i in range(m):
for j in range(n):
isEdge = (i == 0 or j == 0 or i == m - 1 or j == n - 1)
if isEdge and board[i][j] == 'O':
self.bfs(board,i,j)
for i in range(m):
for j in range(n):
if board[i][j] == 'O':
board[i][j] = 'X'
if board[i][j] == '#':
board[i][j] = 'O'
def bfs(self,board,i,j):
queue = collections.deque()
queue.append((i,j))
board[i][j] = '#'
while queue:
x,y = queue.popleft()
#上
if x - 1 >= 0 and board[x - 1][y] == 'O':
queue.append((x-1,y))
board[x-1][y] = '#'
#下
if x + 1 < len(board) and board[x + 1][y] == 'O':
queue.append((x + 1,y))
board[x + 1][y] = '#'
#左
if y - 1 >= 0 and board[x][y - 1] =='O':
queue.append((x,y - 1))
board[x][y- 1] = '#'
#右
if y + 1 < len(board[0]) and board[x][y + 1] == 'O':
queue.append((x,y + 1))
board[x][y + 1] = '#'
3、复杂度分析:
时间复杂度:O(M x N)
空间复杂度:O(M x N)
‘