关于python:BFS与-DFS题目练习python

42次阅读

共计 1975 个字符,预计需要花费 5 分钟才能阅读完成。

 BFS
class Solution:

def numIslands(self, grid: List[List[str]]) -> int:
    if grid is None or len(grid) == 0:
        return 0
    result = 0
    row = len(grid)
    col = len(grid[0])
    queue = []
    for i in range(0, row):
        for j in range(0, col):
            if grid[i][j] == '1':
                result = result + 1
                queue.append([i,j])
                grid[i][j] = '0'
                while len(queue) > 0:
                    cur = queue.pop()
                    x = cur[0]
                    y = cur[1]
                    if x-1 >= 0 and grid[x-1][y] == '1':
                        queue.append([x-1, y])
                        grid[x-1][y] = '0'
                    if y-1 >= 0 and grid[x][y-1] == '1':
                        queue.append([x, y-1])
                        grid[x][y-1] = '0'
                    if x+1 < row and grid[x+1][y] == '1':
                        queue.append([x+1, y])
                        grid[x+1][y] = '0'
                    if y+1 < col and grid[x][y+1] == '1':
                        queue.append([x, y+1])
                        grid[x][y+1] = '0'
    return result

dfs 
class Solution:

def numIslands(self, grid: List[List[str]]) -> int:
    if grid is None or len(grid) == 0:
        return 0
    result = 0
    row = len(grid)
    col = len(grid[0])
    for i in range(0, row):
        for j in range(0, col):
            if grid[i][j] == '1':
                result = result + 1
                self.dfs(grid, i, j, row, col)
    return result
def dfs(self, grid, x, y, row, col):
    if x < 0 or y < 0 or x >= row or y >= col or grid[x][y] == '0':
        return
    grid[x][y] = '0'
    self.dfs[Skrill 下载](https://www.gendan5.com/wallet/Skrill.html)(grid, x - 1, y, row, col)
    self.dfs(grid, x + 1, y, row, col)
    self.dfs(grid, x, y - 1, row, col)
    self.dfs(grid, x, y + 1, row, col)

class Solution:

# Leetcode 200. Number of Islands
# Union Find
# R is the row of grid
# C is the column of grid
# Time Complexity: O(RC)
# Space Complexity: O(RC)
def numIslands(self, grid: List[List[str]]) -> int:
    if grid is None or len(grid) == 0:
        return 0
    row = len(grid)
    col = len(grid[0])
    waters = 0
    uf = UnionFind(grid)
    for i in range(0, row):
        for j in range(0, col):
            if grid[i][j] == '0':
                waters += 1
            else:
                directions = [(0,1), (0,-1), (-1,0), (1,0)]
                for x, y in directions:
                    x = x + i
                    y = y + j
                    if x>=0 and y>=0 and x<row and y<col and grid[x][y] == '1':
                        uf.union(x*col+y, i*col+j)
    return uf.getCount() - waters

class UnionFind:

def __init__(self, grid):
    row = len(grid)
    col = len(grid[0])
    self.root = [-1]*(row*col)
    self.count = row*col
    for i in range(0, row*col):
        self.root[i] = i
def find(self, x):
    if x == self.root[x]:
        return self.root[x]
    else:
        self.root[x] = self.find(self.root[x])
        return self.root[x]
def union(self, x, y):
    rootX = self.find(x)
    rootY = self.find(y)
    if rootX != rootY:
        self.root[rootX] = rootY
        self.count -= 1
def getCount(self):
    return self.count

正文完
 0