zoukankan      html  css  js  c++  java
  • 递归和回溯_leetcode130

    #coding=utf-8
    class Solution(object):
    def solve(self, board):
    """
    :type board: List[List[str]]
    :rtype: void Do not return anything, modify board in-place instead.
    """


    # 思路 : step 1)从边界找所有0的floodfill区域,并标记为visited
    # step 2)所有未被标记的0 置为 x

    if not board:
    return board

    self.derection = [[-1,0],[0,1],[1,0],[0,-1]]

    self.m = len(board)
    self.n = len(board[0])

    # self.m = 4
    # self.n = 4


    self.visit = [[False for i in range(self.n)] for i in range(self.m)]


    for x in range(self.m):
    for y in range(self.n):

    if self.inBoundary(x,y) and not self.visit[x][y] and board[x][y] == "0":
    self.dfs(board,x,y)


    board = self.overTurn(board)
    print board


    def inArea(self,x,y):
    return x >= 0 and x < self.m and y >= 0 and y < self.n

    def inBoundary(self,x,y):

    # self.m = 4
    # self.n = 4

    colFlag = False
    rowFlag = False

    if x in range(self.m):
    colFlag = (y == 0 or y == self.n-1)

    if y in range(self.n):
    rowFlag = ( x == 0 or x == self.m-1)

    return colFlag or rowFlag



    def dfs(self,board,x,y):

    self.visit[x][y] = True

    for item in self.derection:
    newX = x + item[0]
    newY = y + item[1]

    if self.inArea(newX,newY) and not self.visit[newX][newY] and board[x][y] == "0":
    self.dfs(board,newX,newY)

    return

    def overTurn(self,board):
    for x in range(self.m):
    for y in range(self.n):
    if board[x][y] == "0" and not self.visit[x][y]:
    board[x][y] = "x"


    # print board
    return board



    class Solution2(object):
    def solve(self, board):
    """
    :type board: List[List[str]]
    :rtype: void Do not return anything, modify board in-place instead.
    """


    # 思路 : step 1)从边界找所有0的floodfill区域,并标记为visited
    # step 2)所有未被标记的0 置为 x

    if not board:
    return board

    # board[0][0] = "A"

    print board

    self.derection = [[-1,0],[0,1],[1,0],[0,-1]]

    self.m = len(board)
    self.n = len(board[0])


    self.visit = [[False for i in range(self.n)] for i in range(self.m)]


    for x in range(self.m): for y in range(self.n): if self.inBoundary(x,y) and not self.visit[x][y] and board[x][y] == "O": self.dfs(board,x,y) board = self.overTurn(board) # print board def inArea(self,x,y): return x >= 0 and x < self.m and y >= 0 and y < self.n def inBoundary(self,x,y): # self.m = 4 # self.n = 4 colFlag = False rowFlag = False if x in range(self.m): colFlag = (y == 0 or y == self.n-1) if y in range(self.n): rowFlag = ( x == 0 or x == self.m-1) return colFlag or rowFlag def dfs(self,board,x,y): self.visit[x][y] = True for item in self.derection: newX = x + item[0] newY = y + item[1] if self.inArea(newX,newY) and not self.visit[newX][newY] and board[x][y] == "O": self.dfs(board,newX,newY) return def overTurn(self,board): for x in range(self.m): for y in range(self.n): if board[x][y] == "O" and not self.visit[x][y]: board[x][y] = "X" # print board return boards = Solution2()s.solve([["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]])
  • 相关阅读:
    近来几个有用的网站
    军事视频网站
    美军武器命名
    区块链的五个关键要素
    处理多媒体的两个重要工具
    Python re模块将字符串分割为列表
    Python 自动刷新网页
    selenium:chromedriver与chrome版本的对应关系
    怎么批量删除QQ空间说说?
    ssm获取数据库名称
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10557028.html
Copyright © 2011-2022 走看看