码迷,mamicode.com
首页 > 其他好文 > 详细

130 Surrounded Regions

时间:2015-07-14 06:09:45      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

题目: 130 Surrounded Regions

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"

 

130 Surrounded Regions

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4644338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!