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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值