leetcode -- Surrounded Regions -- 典型题。BFS

https://leetcode.com/problems/surrounded-regions/

跟https://leetcode.com/problems/number-of-islands/ 一起看,都是这种要找是否搜索过的题目,要把搜索过的点标记起来。通常可以用一个mask matrix来记录是否已经被记录了。

这道题目的意思就是只要是与边上的O连在一起的O就可以不用被flip。所以这里从所有便开始找其上下左右方向的O,然后赋值为D。搜索完之后,再把棋盘中间的O变成X,D变成O就行了

思路1 DFS 不能过big case

参考
http://www.cnblogs.com/zuoyuan/p/3765434.html

思路2 BFS 可以AC

思路:
http://yucoding.blogspot.hk/2013/08/leetcode-question-131-surrounded-regions.html 这里用的是很经典的写法。

http://www.cnblogs.com/zuoyuan/p/3765434.html
http://blog.csdn.net/ojshilu/article/details/22600449

ref1的code
这里只要是True的话,就不用被flip,值不变。边上的值肯定不变

class Solution:
    # @param board, a 2D array
    # Capture all regions by modifying the input board in-place.
    # Do not return any value.
    def solve(self, board):
        row = len(board)
        if row==0:
            return
        col = len(board[0])    
        bb = [[False for j in xrange(0,col)] for i in xrange(0,row)]
        que = []
        for i in xrange(0,col):
            if board[0][i]=='O':
                bb[0][i]=True
                que.append([0,i])
            if board[row-1][i]=='O':
                bb[row-1][i]=True
                que.append([row-1,i])

        for i in xrange(0,row):
            if board[i][0]=='O':
                bb[i][0]=True
                que.append([i,0])
            if board[i][col-1]=='O':
                bb[i][col-1]=True
                que.append([i,col-1])

        while que:
            i = que[0][0]
            j = que[0][1]
            que.pop(0)
            if (i-1>0 and board[i-1][j]=='O' and bb[i-1][j]==False):
                bb[i-1][j]=True
                que.append([i-1,j])
            if (i+1<row-1 and board[i+1][j]=='O' and bb[i+1][j]==False):
                bb[i+1][j]=True
                que.append([i+1,j])
            if (j-1>0 and board[i][j-1]=='O' and bb[i][j-1]==False):
                bb[i][j-1]=True
                que.append([i,j-1])
            if (j+1<col-1 and board[i][j+1]=='O' and bb[i][j+1]==False):
                bb[i][j+1]=True
                que.append([i,j+1])

        for i in xrange(0,row):
            for j in xrange(0,col):
                if board[i][j]=='O' and bb[i][j]==False:
                    board[i][j] = 'X'

        return

其实这里可以不用mask matrix bb. 像ref2里面一样直接把board当做记录就行。如果访问过 就置为 ’ D’

class Solution(object):
    def solve(self, board):
        """ :type board: List[List[str]] :rtype: void Do not return anything, modify board in-place instead. """
        row = len(board)
        if row==0:
            return
        col = len(board[0])    
        que = []
        for i in xrange(0,col):
            if board[0][i]=='O':
                board[0][i]='D'
                que.append([0,i])
            if board[row-1][i]=='O':
                board[row-1][i]='D'
                que.append([row-1,i])

        for i in xrange(0,row):
            if board[i][0]=='O':
                board[i][0]='D'
                que.append([i,0])
            if board[i][col-1]=='O':
                board[i][col-1]='D'
                que.append([i,col-1])

        while que:
            i = que[0][0]
            j = que[0][1]
            que.pop(0)
            if (i-1>0 and board[i-1][j]=='O' and board[i-1][j] != 'D'):
                board[i-1][j]='D'
                que.append([i-1,j])
            if (i+1<row-1 and board[i+1][j]=='O' and board[i+1][j] != 'D'):
                board[i+1][j]='D'
                que.append([i+1,j])
            if (j-1>0 and board[i][j-1]=='O' and board[i][j-1] != 'D'):
                board[i][j-1]='D'
                que.append([i,j-1])
            if (j+1<col-1 and board[i][j+1]=='O' and board[i][j+1] != 'D'):
                board[i][j+1]='D'
                que.append([i,j+1])

        for i in xrange(0,row):
            for j in xrange(0,col):
                if board[i][j]=='O':
                    board[i][j] = 'X'
                elif board[i][j] == 'D':
                    board[i][j] = 'O'

        return

你可能感兴趣的:(LeetCode)