标签:
bfs搜索即可
from Queue import Queue class Solution: # @param {character[][]} board # @return {void} Do not return anything, modify board in-place instead. def solve(self, board): q = Queue() m = len(board) if m == 0: return n = len(board[0]) for j in range(0, n): if board[0][j] == "O": q.put([0,j]) board[0][j] = "V" if board[m-1][j] == "O": q.put([m-1,j]) board[m-1][j] = "V" for i in range(0, m): if board[i][0] == "O": q.put([i,0]) board[i][0] = "V" if board[i][n-1] == "O": q.put([i,n-1]) board[i][n-1] = "V" while not q.empty(): [px, py] = q.get() for [x,y] in [[px+1, py], [px-1, py], [px, py+1], [px,py-1]]: if 0<=x<m and 0<=y<n and board[x][y] == "O": q.put([x,y]) board[x][y] = "V" for i in range(0, m): for j in range(0,n): if board[i][j] != "V": board[i][j] = "X" else: board[i][j] = "O"
标签:
原文地址:http://www.cnblogs.com/dapanshe/p/4644338.html